-
Notifications
You must be signed in to change notification settings - Fork 130
Description
-
request 传递URL
payload = {'key': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params = payload)
r.url -
定制请求头部
header = {'user-agent': 'safari'}
r = requests.get("http://httpbin.org/get", headers = header)
定制 header 作用:实现接口服务器获取指定header,作为用户识别或全链路跟踪 -
post 表单
r = requests.post('http://httpbin.org/post', data = {'key': 'value'})
r.json()
import json
python_string = {
'name': 'value',
'name2': 'value2'
}
json_string = json.dumps(python_string)
python_strings = json.loads(json_string)
支持 json的数据库mongoDB -
cookies
s = requests.Session() 多次请求固定在一个session,获得相同的 cookies
browser = webdriver.Chrome()
通过 browser.get_cookies() 获取cookie
会话可以用作前后文管理器:
with requests.Session() as s:
s.get('http://*')
下载图片或大文件(stream):
with open("python_logo.png", 'wb') as f:
f.write(r.content)
r = requests.get(file_url, stream = True)
with open("python.pdf", 'wb') as pdf:
for chunk in r.iter_content(chunk_size = 1024):
if chunk:
pdf.write(chunk)
SSL证书:
requests.get('https://kennethreitz.org', verify = False)