安装
pip install requests
请求
.get()
params参数字典表
import requests
url = 'http://httpbin.org/get'
r = requests.get(url)
r.status_code
200
r.text
'{\n "args": {}, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Connection": "close", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.18.4"\n }, \n "origin": "175.167.154.93", \n "url": "http://httpbin.org/get"\n}\n'
d = {'name':'Mace', 'age':20}
r = requests.get(url, params=d)
r.text
'{\n "args": {\n "age": "20", \n "name": "Mace"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Connection": "close", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.18.4"\n }, \n "origin": "175.167.154.93", \n "url": "http://httpbin.org/get?name=Mace&age=20"\n}\n'
r.url
'http://httpbin.org/get?name=Mace&age=20'
.post()
data数据字典表
json要提交的json数据
url = 'http://httpbin.org/post'
d
{'name': 'Mace', 'age': 20}
r = requests.post(url, data=d)
r.text
'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "age": "20", \n "name": "Mace"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Connection": "close", \n "Content-Length": "16", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.18.4"\n }, \n "json": null, \n "origin": "175.167.154.93", \n "url": "http://httpbin.org/post"\n}\n'
响应
类型 requests.models.Response
- .status_code 状态码
- .ok 是否成功
- .encoding 编码
- .apparent_encoding 表现编码
- .content 内容文本
- .text 内容文本
- .json() 内容json
- .history 重定向历史
import requests
url = 'http://httpbin.org/get'
d = {'name':'mace', 'age':20}
r = requests.get(url, params=d)
type(r)
<class 'requests.models.Response'>
r.status_code
200
r.ok
True
r.encoding
r.apparent_encoding
'ascii'
r.text
'{\n "args": {\n "age": "20", \n "name": "mace"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Connection": "close", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.18.4"\n }, \n "origin": "175.167.154.93", \n "url": "http://httpbin.org/get?name=mace&age=20"\n}\n'
r.json()
{'args': {'age': '20', 'name': 'mace'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.4'}, 'origin': '175.167.154.93', 'url': 'http://httpbin.org/get?name=mace&age=20'}
自定义 headers
.get(url, headers={'key':'value'})
import requests
url = 'http://httpbin.org/get'
r = requests.get(url)
print(r.text)
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4"
},
"origin": "175.167.154.93",
"url": "http://httpbin.org/get"
}
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'}
r = requests.get(url, headers=headers)
print(r.text)
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
},
"origin": "175.167.154.93",
"url": "http://httpbin.org/get"
}
headers['Referer'] = 'httpbin.org'
headers
{'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36', 'Referer': 'httpbin.org'}
r = requests.get(url, headers=headers)
print(r.text)
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Host": "httpbin.org",
"Referer": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
},
"origin": "175.167.154.93",
"url": "http://httpbin.org/get"
}
cookies 操作
- jar = requests.cookies.RequestsCookieJar()
- jar.set('key', 'value')
- requests.get(url, cookies=jar)