Python3使用requests
2019-06-13
7 min read
1. 发送请求
import requests
r = requests.get(url='http://httpbin.org/get')
r = requests.post('http://httpbin.org/post')
r = requests.put('http://httpbin.org/put')
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/get')
r = requests.options('http://httpbin.org/get')
2. 设置UA
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'}
r = requests.get(url='http://httpbin.org/get', headers=headers)
print(r.text)
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36"
},
"origin": "180.172.125.179, 180.172.125.179",
"url": "https://httpbin.org/get"
}
3. 设置代理
import requests
proxies = {'http': 'http://47.100.233.103:10250',
'https': 'https://47.100.233.103:10250'}
response = requests.get('https://www.12306.cn', proxies=proxies)
print(response.status_code)
# HTTP Basic Auth
import requests
proxies = {'http': 'http://user:password@47.100.233.103:10250',
'https': 'https://user:password@47.100.233.103:10250'}
response = requests.get('https://www.12306.cn', proxies=proxies)
print(response.status_code)
# SOCKS 协议代理
import requests
proxies = {'http': 'socks5://user:password@host:port',
'https': 'socks5://user:password@host:port'}
response = requests.get('https://www.12306.cn', proxies=proxies)
print(response.status_code)
4. 身份认证
# 默认使用 HTTPBasicAuth 这个类来认证
import requests
r = requests.get('http://192.168.30.229:5000', auth=HTTPBasicAuth('username', 'password'))
print(r.status_code)
# OAuth 认证
import requests
from requests_oauthlib import OAuth1
url = 'https://api.twitter/1.1/account/verify_credentials.json'
auth = OAuth1('YOUR_APP _KEY', ' YOUR_APP_SECRET',
'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET')
requests.get(url, auth=auth)
5. 设置超时
import requests
response = requests.get('https://www.taobao.com', timeout=0.01)
print(response.status_code)
# 请求分为两个阶段,即连接(connect)和读取(read)。
# 设置的 timeout 将用作连接和读取这二者的 timeout 总和。
# 如果要分别指定,就可以传入一个元组:
import requests
response = requests.get('https://www.taobao.com', timeout=(5, 11))
print(response.status_code)
6. get传参
import requests
params = {'name': 'aici', 'age': 18}
r = requests.get(url='http://httpbin.org/get', params=params)
print(r.text)
{
"args": {
"age": "18",
"name": "aici"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"origin": "180.172.125.179, 180.172.125.179",
"url": "https://httpbin.org/get?name=aici&age=18"
}
7. post请求传参
import requests
data = {'name': 'aici', 'age': 18}
r = requests.post(url='http://httpbin.org/post', data=data)
print(r.text)
{
"args": {},
"data": "",
"files": {},
"form": {
"age": "18",
"name": "aici"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "16",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": null,
"origin": "180.172.125.179, 180.172.125.179",
"url": "https://httpbin.org/post"
}
8. 文件上传
import requests
files = {'file': open('z.py', 'rb')}
r = requests.post(url='http://httpbin.org/post', files=files)
print(r.text)
{
"args": {},
"data": "",
"files": {
"file": "import requests\r\n\r\nfiles = {'file': open('z.py', 'rb')}\r\nr = requests.post(url='http://httpbin.org/post', files=files)\r\nprint(r.text)\r\n"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "275",
"Content-Type": "multipart/form-data; boundary=845dbe8cf3e79f9268c1126c637eb858",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": null,
"origin": "180.172.125.179, 180.172.125.179",
"url": "https://httpbin.org/post"
}
9. 会话维持
import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456789')
r = s.get('http://httpbin.org/cookies')
print(r.text)
{
"cookies": {
"number": "123456789"
}
}
10. Prepared Request
```
'''
将请求表示为数据结构, 其中各个参数都可以通过一个 Request 对象来表示。
引入Request对象, 然后用url、data和headers参数构造了一个Request对象, 这时需要再调用Session的prepare_request()方法将其转换为一个Prepared Request 对象, 然后调用send()方法发送即可。
'''
from requests import Request, Session
url = 'http://httpbin.org/post'
data = {
'name': 'germey'
}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'}
s = Session()
req = Request('POST', url, data=data, headers=headers)
prepped = s.prepare_request(req)
r = s.send(prepped)
print(r.text)
```
11. https的ssl证书验证
```
忽略警告方法一:
import requests
import urllib3
urllib3.disable_warnings()
response = requests.get('https://www.12306.cn', verify=False)
print(response.status_code)
忽略警告方法二:
import logging
import requests
logging.captureWarnings(True)
response = requests.get('https://www.12306.cn', verify=False)
print(response.status_code)
证书验证(本地私有证书的key必须是解密状态,加密状态的key是不支持的):
import requests
response = requests.get('https://www.12306.cn', cert=('/path/server.crt', '/path/key'))
print(response .status_code)
```