requests常用请求(get、post json)
'''
requests常用请求(get、post json)
'''
import requests
import json
get
params = {"a": "xxx", "b": "xxx"}
url = "http://..."
r = requests.get(url, params=params)
print r.status_code
post base
url = "http://..."
data = {
"name": "xxx",
"email": "[email protected]"
}
data = (('key1', 'value1'), ('key1', 'value2'))
r = requests.post(url=url, data=data)
print r.status_code
post json
url = "http://..."
data = {
"name": "xxx",
"email": "[email protected]"
}
headers = {'Content-Type': 'application/json'}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
r = requests.post(url=url, json=data)
print r.status_code
delete
r = requests.request("DELETE", url, data="", headers=headers)
request参数
params = {"a": "xxx", "b": "xxx"}
url = "http://..."
r = requests.get(url, params=params, timeout=0.001)
r = requests.get(url, params=params, auth=('username', 'password'))
响应内容
print r.status_code
print requests.codes.ok
r.raise_for_status()
print r.url
print r.encoding
print r.text
print r.content
print r.json()
json.loads(r.content)
print r.headers
print r.headers['content-length']