Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythoner进行网络请求时,可以轻而易举的完成浏览器可有的任何操作。
1.get请求
import requests
url = 'https://www.baidu.com/'
# 不带参数
response = requests.get(url)
print(response.url) # https://www.baidu.com/
# 带参数
data = {
'key1': 'value1',
'key2': 'value2'
}
response = requests.get(url, params=data)
print(response.url) # https://www.baidu.com/?key1=value1&key2=value2
2.post请求
import requests
url = 'https://www.baidu.com/'
data = {
'key1': 'value1',
'key2': 'value2'
}
# 带参数
response = requests.post(url, data=data)
print(response.url) # https://www.baidu.com/
# 发送json数据
import json
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(data), headers=headers)
3.其他请求
requests.get(url, params=None, **kwargs)
requests.post(url, data=None, json=None, **kwargs)
requests.put(url, data=None, **kwargs)
requests.head(url, **kwargs)
requests.delete(url, **kwargs)
requests.patch(url, data=None, **kwargs)
requests.option(url, **kwargs)
# 以上方法是基于此方法构建
requests.request(method, url, **kwargs)
# requests.request(method='get', url)
4.其他参数
###### 1.headers ######
headers = {
'user-agent': '...',
'content-type': 'application/json'
...
}
requests.post(url, headers=headers)
###### 2.cookies ######
cookies = {'c1': 'v1'}
requests.post(url, cookies=cookies)
# 也可以使用CookieJar(字典形式的cookies就是在此基础上进行的封装)
from http.cookiejar import CookieJar
from http.cookiejar import Cookie
obj = CookieJar()
obj.set_cookie(Cookie(
version=0, name='user', value='pwd', port=None,
domain='', path='/', secure=False, expires=None,
discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False,
port_specified=False, domain_specified=False, domain_initial_dot=False, path_specified=False)
)
requests.post(url, data={'c1': 'v1'}, cookies=obj)
###### 3.params ######
# 可以是字典;可以是字符串;也可以是字节
requests.get(url, params={'k1': 'v1', 'k2': 'v2'})
requests.get(url, params='k1=v1&k2=v2')
requests.get(url, params=bytes('k1=v1&k2=v2', encoding='utf8'))
###### 4.data ######
# 可以是字典;可以是字符串;可以是字节;可以是文件对象
requests.post(url, data={'k1': 'v1', 'k2': 'v2'})
requests.post(url, data='k1=v1; k2=v2')
data = 'k1=v1;k2=v2'
requests.post(url, data='k1=v1;k2=v2', headers = {'content-type': 'application/x-www-form-urlencoded'})
requests.post(url, data=open('file.text', 'r', encoding='utf8'))
###### 5.json ######
# 内部进行序列化并作为请求体发送,自动设置content-type为aplication/json
requests.post(url, json={'k1': 'v1', 'k2': 'v2'})
###### 6.post发送文件 ######
# 普通发送文件
file_dict = {
file_dict = {
'f1': open('text.txt', 'rb')
}
requests.post(url, files=files_dict)
# 自定义文件名1
file_dict = {
'f1': ('自定义文件名.txt', open('text.txt', 'rb'))
}
requests.post(url, files=files_dict)
# 自定义文件名2
file_dict = {
'f1': ('text.txt', '自定义文件名.txt')
}
requests.post(url, files=files_dict)
###### 7.auth认证 ######
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
requests.get(url, auth=HTTPBasicAuth('user', 'pwd'))
requests.get(url, auth=HTTPDigesAuth('user', 'pwd'))
###### 8.timeout超时操作 ######
# 传入一个数字:连接最大时间
requests.get(url, timeout=1)
# 传入一个元组:连接最大时间, 接收数据最大时间
requests.get(url, timeout=(3, 3))
###### 9.poxies代理 ######
# 一般代理(自动检测http和https,并取得对应的代理)
proxies = {
'http': '127.0.0.1:8888',
'https': '127.0.0.1:8888'
}
requests.get(url, proxies=proxies)
# 专属代理(指定地址使用专属代理)
proxies = {'https://www.baidu.com/': 'http': '127.0.0.1:8888'}
requests.get(url, proxies=proxies)
# 加密代理
from requests.auth import HTTPProxyAuth
proxies= {
'http': '127.0.0.1:8888',
'https': '127.0.0.1:8888'
}
auth = HTTPProxyAuth('user', 'pwd')
requests.get(url, proxies=proxies, auth=auth)
###### 10.stream下载文件 ######
# stream = True: 文件一点一点读取
from contextlib import closing
with closing(requests.get(url, stream=True)) as f:
for i in f.iter_content():
print(i)
###### 11.allow_redirects重定向 ######
allow_redirect = False: 不允许重定向
requests.get(url, allow_redirect=False)
###### 12.session ######
# session会自定携带cookies和请求头数据
session = requests.Session()
res = session.get(url) # 已获取在此页面的cookies, 下个请求自动带此数据
res_2 = session.get(url) # 在这个url会对cookie进行检测,只有携带第一个cookies才能登陆,使用session可以自动携带