Python3.x:requests的用法
Python3.x:requests的用法
1,requests 比 urllib.request 容错能力更强;
2,通常用法:
(1).认证、状态码、header、编码、json
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
r.status_code
r.headers['content-type']
输出:'application/json; charset=utf8'
r.encoding
输出:'utf-8'
r.text
输出:u'{"type":"User"...'
r.json()
输出:{u'private_gists': 419, u'total_private_repos': 77, ...}
(2).发起请求
import requests
URL="http://www.bsdmap.com/"
#
r = requests.get(URL)
#
r = requests.post(URL)
#
r = requests.put(URL)
#
r = requests.delete(URL)
#
r = requests.head(URL)
#
r = requests.options(URL)
(3).通过URL传递参数
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
print r.url
输出:u'http://httpbin.org/get?key2=value2&key1=value1'
(4).返回内容
import requests
r = requests.get('https://github.com/timeline.json')
r.text
输出:'[{"repository":{"open_issues":0,"url":"https://github.com/...
r.encoding
输出:'utf-8'
r.encoding = 'ISO-8859-1'
(5).二进制内容
r.content
输出:b'[{"repository":{"open_issues":0,"url":"https://github.com/... from PIL import Image
from StringIO import StringIO
i = Image.open(StringIO(r.content))
(6).JSON
import requests
r = requests.get('https://github.com/timeline.json')
r.json()
输出:[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
(7).超时
requests.get('http://github.com', timeout=0.001)
(8).自定义header
import json
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'} r = requests.post(url, data=json.dumps(payload), headers=headers)
官方文档:http://docs.python-requests.org/en/latest/user/quickstart/
Python3.x:requests的用法的更多相关文章
- python3使用requests登录人人影视网站
python3使用requests登录人人影视网站 继续练习使用requests登录网站,人人影视有一项功能是签到功能,需要每天登录签到才能升级. 下面的代码python代码实现了使用requests ...
- python3使用requests发闪存
闪存ing.cnblogs.com是博客园类似推特.饭否的服务, 我写了以下程序可以完成发闪存的操作,目的是顺便练习使用requests库. requests是一个python 轻量的http客户端库 ...
- Python3下requests库发送multipart/form-data类型请求
[本文出自天外归云的博客园] 要模拟multipart/form-data类型请求,可以用python3的requests库完成.代码示例如下: #请求的接口url url = "url&q ...
- Python3:Requests模块的异常值处理
Python3:Requests模块的异常值处理 用Python的requests模块进行爬虫时,一个简单高效的模块就是requests模块,利用get()或者post()函数,发送请求. 但是在真正 ...
- 爬虫—Requests高级用法
Requests高级用法 1.文件上传 我们知道requests可以模拟提交一些数据.假如有的网站需要上传文件,我们也可以用requests来实现. import requests files = { ...
- Python3之Requests模块详解
# 导入 Request模块 # 若本机无自带Request模块,可自行下载或者使用pip进行安装 # python版本Python3 import requests import json #### ...
- Python爬虫十六式 - 第三式:Requests的用法
Requests: 让 HTTP 服务人类 学习一时爽,一直学习一直爽 Hello,大家好,我是Connor,一个从无到有的技术小白.今天我们继续来说我们的 Python 爬虫,上一次我们说到了 ...
- 爬虫简介、requests 基础用法、urlretrieve()
1. 爬虫简介 2. requests 基础用法 3. urlretrieve() 1. 爬虫简介 爬虫的定义 网络爬虫(又被称为网页蜘蛛.网络机器人),是一种按照一定的规则,自动地抓取万维网信息的程 ...
- 转:【Python3网络爬虫开发实战】 requests基本用法
1. 准备工作 在开始之前,请确保已经正确安装好了requests库.如果没有安装,可以参考1.2.1节安装. 2. 实例引入 urllib库中的urlopen()方法实际上是以GET方式请求网页,而 ...
随机推荐
- Date Json格式转换Date格式
CreateTime=\/Date(1458722493663+0800)\/ var CreateTime="/Date(1458722493663+0800)/";var st ...
- $.when()方法监控ajax请求获取到的数据与普通ajax请求回调获取到的数据的不同
1.$.when(ajax).done(function(data)}); 2.$.ajax().done(function(data){}); 1中的data被封装进一个对象[data, " ...
- Python学习之k-近邻算法
1. K-近邻算法 # coding=utf-8 from numpy import * import operator def createDataSet(): group = array([[1. ...
- 在CentOS中使用 yum 安装MongoDB及服务器端配置
转自 http://blog.csdn.net/zhangfeng19880710/article/details/20166853 一.准备工作: 运行yum命令查看MongoDB的包信息 [roo ...
- shell脚本分析nginx日志
shell脚本分析nginx日志: name=`awk -F ',' '{print $13":"$32}' $file | awk -F ':' '{print $4}'`ech ...
- 大规模网站sesson会话保持思路及实践配置
1.需求: 请教个问题:我用lvs的rr模式,(考虑过 ipvsadm -p,但是这样就失去了负载均衡的意义感觉),后端有10台web,用一台memcached专门用于存储session文件,但是现在 ...
- Oracle(四):锁
v$lock:视图列出当前系统持有的或正在申请的所有锁的情况 v$locked_object:视图列出当前系统中哪些对象正被锁定 1).查询当前数据库锁的情况,以及导致锁的sql语句: Select ...
- c#中类和对象详解
1.1 类和对象 类 (class) 是最基础的 C# 类型.类是一个数据结构,将状态(字段)和操作(方法和其他函数成员)组合在一个单元中.类为动态创建的类实例 (instance) 提供了定义,实例 ...
- AttributeError: 'module' object has no attribute 'face'
报错 raceback (most recent call last): File "D:/work/python/face_ai/predict.py", line 41, in ...
- python redis基本概念简单操作
转自:http://www.cnblogs.com/melonjiang/p/5342383.html 一.redis redis是一个key-value存储系统.和Memcached类似,它支持存储 ...