python-requests包
Posted su_sir
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-requests包相关的知识,希望对你有一定的参考价值。
1.作用
提供了HTTP协议的所有请求方式(如get,post,put,delete等)
2.安装
pip install requests
3.常见用法
-
发送GET请求(不传参数)
>>> import requests
>>> resp = requests.get(‘https://www.baidu.com‘)
-
发送GET请求(传递参数)
>>> import requests
>>> params = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘}
>>> resp = requests.get("http://httpbin.org/get", params=params)
>>> print(resp.url)
http://httpbin.org/get?key2=value2&key1=value1
-
发送POST请求
>>> import requests
>>> resp = requests.post(‘http://httpbin.org/post‘, data = {‘key‘:‘value‘})
-
发送其他类型的请求
>>> resp = requests.put(‘http://httpbin.org/put‘, data = {‘key‘:‘value‘})
>>> resp = requests.delete(‘http://httpbin.org/delete‘)
>>> resp = requests.head(‘http://httpbin.org/get‘)
>>> resp = requests.options(‘http://httpbin.org/get‘)
-
发送请求时,可以设置:Headers,Cookies,代理,禁止证书验证,超时
设置Headers
>>> url = ‘https://api.github.com/some/endpoint‘
>>> headers = {‘user-agent‘: ‘my-app/0.0.1‘}
>>> resp = requests.get(url, headers=headers)
设置Cookies
>>> url = ‘http://httpbin.org/cookies‘
>>> cookies = {‘cookies_are‘: ‘working‘}
?
>>> resp = requests.get(url, cookies=cookies)
>>> resp.text
‘{"cookies": {"cookies_are": "working"}}‘
设置代理
import requests
?
proxies = {
‘http‘: ‘http://10.10.1.10:3128‘,
‘https‘: ‘http://10.10.1.10:1080‘,
}
?
requests.get(‘http://example.org‘, proxies=proxies)
设置禁止证书验证
>>> import requests
>>> resp = requests.get(‘http://httpbin.org/post‘, verify=False)
设置超时
>>> requests.get(‘http://github.com‘, timeout=0.001)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
requests.exceptions.Timeout: HTTPConnectionPool(host=‘github.com‘, port=80): Request timed out. (timeout=0.001)
-
获取响应内容
Requests
通过
text属性,就可以获得字符串格式的响应内容。
>>> import requests
?
>>> resp = requests.get(‘https://api.github.com/events‘)
>>> resp.text
u‘[{"repository":{"open_issues":0,"url":"https://github.com/...
Requests通过
content属性,就可以获得二进制数据格式的响应内容。
>>> resp.content
b‘[{"repository":{"open_issues":0,"url":"https://github.com/...
Requests通过
json方法,就可以获得JSON格式的响应内容。
>>> import requests
?
>>> resp = requests.get(‘https://api.github.com/events‘)
>>> resp.json()
[{u‘repository‘: {u‘open_issues‘: 0, u‘url‘: ‘https://github.com/...
-
获取响应参数:响应状态码、响应报文头、url、服务器返回的cookies、响应的Session、
获取响应状态码,通过status_code属性获取
>>> resp = requests.get(‘http://httpbin.org/get‘)
>>> resp.status_code
200
获取响应报文头,通过headers属性获取
>>> r.headers
{
‘content-encoding‘: ‘gzip‘,
‘transfer-encoding‘: ‘chunked‘,
‘connection‘: ‘close‘,
‘server‘: ‘nginx/1.0.4‘,
‘x-runtime‘: ‘148ms‘,
‘etag‘: ‘"e1ca502697e5c9317743dc078f67693f"‘,
‘content-type‘: ‘application/json‘
}
获取服务器返回的cookies,通过cookies属性获取,得到是一个字典
>>> url = ‘http://example.com/some/cookie/setting/url‘
>>> resp = requests.get(url)
>>> resp.cookies[‘example_cookie_name‘]
‘example_cookie_value‘
获取响应的url,通过url属性获取
>>> import requests
>>> params = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘}
>>> resp = requests.get("http://httpbin.org/get", params=params)
>>> print(resp.url)
http://httpbin.org/get?key2=value2&key1=value1
-
设置session(上边有设置cookie方法了)
import requests
?
session = requests.Session() # 建立一个session
?
session.get(‘http://httpbin.org/cookies/set/sessioncookie/123456789‘) # 设置session
resp = session.get(‘http://httpbin.org/cookies‘)
?
print(resp.text)
# ‘{"cookies": {"sessioncookie": "123456789"}}‘
(如果你想要让一个headers
在Session
的整个生命周期内都有效的话,需要用以下的方式来进行设置:)
# 设置整个headers
session.headers = {
‘user-agent‘: ‘my-app/0.0.1‘
}
# 增加一条headers
session.headers.update({‘x-test‘: ‘true‘})
以上是关于python-requests包的主要内容,如果未能解决你的问题,请参考以下文章
XHR 请求在 Scrapy 中失败,但在 python-requests 中有效