1. 请求方式
requests.get(url)
requests.post(url)
requests.put(url)
requests.delete(url)
requests.head(url)
requests.options(url)
1.1 requests.get()
-
基本用法
import requests resp = requests.get('http://httpbin.org/get') print(resp.text)
-
带参数的
get
用法import requests data = { 'name': 'yindf', 'age': 24 } resp = requests.get('http://httpbin.org/get', params=data) print(resp.text)
-
解析
json
import requests import json resp = requests.get('http://httpbin.org/get') print(resp.text) print(type(resp.text)) print(resp.json()) print(type(resp.json())) print(json.loads(resp.text)) print(type(json.loads(resp.text)))
resp.json()
其实就是调用了json.loads(resp.text)
,所以二者结果一致 -
获取二进制数据(下载图片、视频等常用)
import requests resp = requests.get('https://github.com/favicon.ico') print(type(resp.text), type(resp.content)) print(resp.text) print(resp.content)
保存二进制数据
import requests resp = requests.get('https://github.com/favicon.ico') with open('favicon.ico', 'wb') as f: f.write(resp.content) f.close()
-
添加
headers
import requests headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1' } resp = requests.get('https://www.zhihu.com/explore', headers=headers) print(resp.text)
1.2 request.post()
-
添加数据与
headers
import requests data = { 'name' : 'yindf', 'age' : 24 } headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1' } resp = requests.post('http://httpbin.org/post', data=data, headers=headers) print(resp.text)
2. 响应
2.1 response
属性
import requests
resp = requests.get('http://www.jianshu.com')
print(type(resp.status_code), resp.status_code)
print(type(resp.headers), resp.headers)
print(type(resp.cookies), resp.cookies)
print(type(resp.url), resp.url)
print(type(resp.history), resp.history)
>> <class 'int'> 200
>> <class 'requests.structures.CaseInsensitiveDict'> {'Date': 'Wed, 20 Dec 2017 01:35:32 GMT', 'Server': 'Tengine', 'Content-Type': 'text/html; charset=utf-8', 'Transfer-Encoding': 'chunked', 'X-Frame-Options': 'DENY', 'X-XSS-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'ETag': 'W/"75c89fd881f59b584e1023df6a772ebd"', 'Cache-Control': 'max-age=0, private, must-revalidate', 'Set-Cookie': 'locale=zh-CN; path=/', 'X-Request-Id': 'cd5c79bc-c9e3-4eb6-9c9b-f40a7f4b7c6e', 'X-Runtime': '0.006088', 'Content-Encoding': 'gzip', 'X-Via': '1.1 PShbwhjyw5dx20:1 (Cdn Cache Server V2.0)', 'Connection': 'keep-alive'}
>> <class 'requests.cookies.RequestsCookieJar'> <RequestsCookieJar[<Cookie locale=zh-CN for www.jianshu.com/>]>
>> <class 'str'> http://www.jianshu.com/
>> <class 'list'> []
3. 高级操作
3.1 文件上传
import requests
files = {
'favicon.ico' : open('favicon.ico', 'rb')
}
resp = requests.post('http://httpbin.org/post', files=files)
print(resp.text)
3.2 获取cookie
import requests
resp = requests.get('https://www.baidu.com')
print(resp.cookies)
for key, val in resp.cookies.items():
print(key+'='+val)
3.3 会话维持,模拟登录
import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123')
resp = s.get('http://httpbin.org/cookies')
print(resp.text)
3.4 证书验证
import requests
resp = requests.get('https://www.12306.cn', verify=False)
print(resp.status_code)
- 这里指定
verify
参数为False
取消证书验证
import requests
resp = requests.get('https://www.12306.cn', cert=('/path/server.crt', '/path/key'))
print(resp.status_code)
- 这里通过
cert
参数指定证书路径
3.5 超时设置
import requests
from requests.exceptions import Timeout
try:
resp = requests.get('https://www.taobao.com',timeout=1)
print(resp.status_code)
except Timeout:
print('Time out')
3.6 认证设置
import requests
from requests.auth import HTTPBasicAuth
resp = requests.get(url, auth=HTTPBasicAuth('user', 'passwd'))
print(resp.status_code)
import requests
resp = requests.get(url, auth=('user', 'passwd'))
print(resp.status_code)
3.7 异常处理
import requests
from requests.exceptions import Timeout, HTTPError, RequestException
try:
resp = requests.get('http://httpbin.org/get', timeout=0.1)
print(resp.status_code)
except Timeout:
print('time out')
except HTTPError:
print('http error')
except RequestException:
print('error')