Request是基于urllib编写、采用Apache2 Licensed开源协议的HTTP库,在使用方面Requests比urllib更加方便
requests.get(url [, params={‘key’: ‘value’}])
注意:
import requests
# 无查询字符串
res1 = requests.get('http://www.baidu.com')
# 有查询字符串
data2 = {
'key1': 'value1',
'key2': 'value2'
}
res2 = requests.get('http://www.xxx.com', params=data)
print(res2) # http://www.xxx.com?key1=value1&key2=value2
# 查询字符串中有列表
data3 = {
'key1': 'value1',
'key2': ['value2', 'value3']
}
res3 = requests.get('http://www.xxx.com', params=data)
print(res3) # http://www.xxx.com?key1=value1&key2=value2&key2=value3
requests.post(url [, data={‘key’: ‘value’}, json={‘key’: ‘value’}])
import requests
url = 'http://www.xxx.com'
# 传递字典
data1 = {
'name': '12345',
'age': '23'
}
res1 = requests.post(url, data=data1)
# 传递元组
data2 = (('key1', 'value1'), ('key1', 'value2'))
res2 = requests.post(url, data=data2)
import json
data3 = {'some': 'data'}
res3 = requests.post(url, data=json.dumps(payload))
# 数据未编码
data4 = {'some': 'data'}
res4 = requests.post(url, json=data4)
requests.put(url [, data={‘key’: ‘value’}])
以字典形式提交表单数据
import requests
url = 'http://www.xxx.com'
data = {
'name': '12345',
'age': '23'
}
res = requests.put(url, data=data)
requests.delete(url)
import requests
res = requests.delete(url)
requests.head(url)
import requests
res = requests.head(url)
requests.options(url)
import requests
res = requests.options(url)
注意:定制header的优先级低于某些特定的信息源
import requests
url = 'http://www.baidu.com'
headers = {'user-agent': 'xxxxxxxxxxxxxx'}
res = requests.get(url, headers=headers)
使用requests方法后,会返回 respone 对象,存储了服务器相应内容
import requests
res - requests.get('https://api.github.com/events')
pirnt(res.text) # u'[{"repository":{"open_issues":0,"url":"https://github.com/...
import requests
res - requests.get('https://api.github.com/events')
pirnt(res.content) # b'[{"repository":{"open_issues":0,"url":"https://github.com/...
# 请求返回的二进制数据创建一张图片
from PIL import Image
from io import BytesIO
i = Image.open(BytesIO(res.content))
注意:调用 res.json() 成功不意味着响应成功(有时响应失败也会包含一个JSON对象)
import requests
res - requests.get('https://api.github.com/events')
pirnt(res.json()) # [{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
res = requests.get('https://api.github.com/events', stream=True)
print(res.raw) #
print(res.raw.read(10)) # '\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'
# 一般情况下,以下面的模式将文本刘保存到文件
with open(filename, 'wb') as fd:
for chunk in r.iter_content(chunk_size):
fd.write(chunk)
res = resquests.get('http://www.xxx.com')
print(res.status_code) # 200
print(res.status_code == requests.code.ok) # True
res.raise_for_status():抛出异常
res.headers:查看服务器响应头
res.cookies:访问响应中的cookie
res.history:追踪重定向
使用 respone.cookies 访问服务器响应中包含的cookie
url = 'http://www.xxx.com'
res = requests.get(url)
print(res.cookies)
url = 'http://www.xxx.com'
cookies1 = {'key': 'value'}
res1 = requests.get(url, cookies.cookies1)
cookies2 = requests.cookies.RequestsCookieJar()
cookies2.set('key', 'value', domain='...', path='...') # domain:有效域,path:有效路径
res2 = resquests.get(url, cookies=cookies2)
发型POST请求时,使用 files 文件
url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}
res = requests.post(url, files=files)
url = 'http://httpbin.org/post'
files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
res = requests.post(url, files=files)
url = 'http://httpbin.org/post'
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
res = requests.post(url, files=files)
注意:
所有显示抛出的异常都继承自Requests.exceptions.RequestException