Python-requests模块
一、模块说明
Requests是使用Apache2 licensed 许可证的HTTP库;
是一个基于Python开的Http库,其在Python内置模块的基础上进行了高度的封装,从而我们在使用Http请求的时候变得非常的简单;
Request支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动响应内容的编码,支持国际化的URL和POST数据自动编码。
二、环境安装和导入
pip install requests
import requests
三、请求发送
本文主要说明get与post请求:
import requests
page_get = requests.get('https://www.google.com') # 不带参数的get请求
page_params = requests.get(url='http://dict.baidu.com/s', params={'wd': 'python'}) # 带参数的get请求
page_get = requests.post('https://www.google.com') # 不带参数的post请求
page_params = requests.get(url='http://dict.baidu.com/s', data={'wd': 'python'}) # 带参数的post请求
其余请求方式:
1 requests.get(‘www.google.com’) # GET请求
2 requests.post(“http://useinfo/list/post”) # POST请求
3 requests.put(“http://useinfo/list/put”) # PUT请求
4 requests.delete(“http://useinfo/list/delete”) # DELETE请求
5 requests.head(“http://useinfo/list/head”) # HEAD请求
6 requests.options(“http://http://useinfo/list/option” ) # OPTIONS请求
四、UA伪装 & url的参数
# 指定url
post_url = 'https://fanyi.baidu.com/v2transapi?from=en&to=zh'
# post请求参数处理(与get请求一致,需注意的是get请求的参数为params)
data = {
'query': 'cat',
'from': 'en',
'to': 'zh',
'transtype': 'realtime',
'simple_means_flag': 3,
'token': '5447540541bd83b3213bc2e9c236e08a',
'sign': 661701.982004,
'domain': 'common',
}
# UA伪装
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'
}
# 发送请求
response = requests.post(url=post_url, data=data, headers=headers)
五、响应内容
response.encoding #获取当前的编码
response.encoding = 'utf-8' #设置编码
response.text #以encoding解析返回内容。字符串方式的响应体,会自动根据响应头部的字符编码进行解码。
response.content #以字节形式(二进制)返回。字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩。
response.headers #以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None
response.status_code #响应状态码
response.ok # 查看response.ok的布尔值便可以知道是否登陆成功
response.json() #Requests中内置的JSON解码器,以json形式返回,前提返回的内容确保是json格式的,不然解析出错会抛异常
response.raise_for_status() #失败请求(非200响应)抛出异常
六、proxies代理
需要使用代理,可以通过为任意请求方法提供 proxies 参数来配置单个请求:
proxies = {
"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080",
}
requests.get("http://example.org", proxies=proxies)
如果代理需要auth验证,也可以使用 http://user:password@host/ 语法:
proxies = {
"http": "http://user:pass@10.10.1.10:3128/",
}
七、session
创建一个会话,并且在会话中保存cookie值,那所有通过该会话发出去的请求都含有会话中保存的cookie;
import requests
response=requests.get("http://httpbin.org/cookies/set/sessioncookie/python") #设置一个name为sessioncookie 值为python的cookie值
response=requests.get('http://httpbin.org/cookies') #获取cookie值
print(response.text) #返回结果为空
s = requests.Session()
s.cookies.set("sessioncookie","value") #在会话中设置一个name为sessioncookie 值为python zhi shi xue tang的cookie值
r = s.get("http://httpbin.org/cookies") #通过会话使用Get方式获取所有的cookie
print(r.text) #获取到cookie值
八、示例代码
import requests
import json
if __name__ == '__main__':
get_url = 'https://movie.douban.com/j/chart/top_list?'
param = {
'type': 24,
'interval_id': '100:90',
'action': '',
'start': '0',
'limit': '20',
}
# UA伪装
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'
}
response = requests.get(url=get_url, params=param, headers=headers)
list_data = response.json()
print(list_data)
fp = open('download/douban.json', 'w', encoding='utf-8')
json.dump(list_data, fp=fp, ensure_ascii=False)
print('ova!!!')