requests模块
1. requests模块的作用
-
发送http请求,获取数据响应
-
实例
import requests # 目标url url = 'https://www.baidu.com' # 向目标url发送get请求 response = requests.get(url) # 打印响应内容 print(response.text)
2. response响应对象
-
网络传输对象都是bytes类型的,所以response.text相当于response.content.decode(‘推测出来的编码字符集’),因为是推测的,所以存在不准确的情况
-
response.content.decode(’’)可以解决不准确的问题,因为他需要手动解码
3. response响应对象的常用方法
-
实例
# 1.2.3-response其它常用属性 import requests # 目标url url = 'https://www.baidu.com' # 向目标url发送get请求 response = requests.get(url) # 打印响应内容 # print(response.text) # print(response.content.decode()) # 注意这里! print(response.url) # 打印响应的url print(response.status_code) # 打印响应的状态码 print(response.request.headers) # 打印响应对象的请求头 print(response.headers) # 打印响应头 print(response.request._cookies) # 打印请求携带的cookies print(response.cookies) # 打印响应中携带的cookies
4. 携带请求头发送请求
-
实例
import requests url = 'https://www.baidu.com' headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"} # 在请求头中带上User-Agent,模拟浏览器发送请求 response = requests.get(url, headers=headers) print(response.content) # 打印请求头信息 print(response.request.headers)
5. cookies参数的作用
- 网站经常利用请求头中的Cookie字段来做用户访问状态的保持,那么我们可以在headers参数中添加Cookie,模拟普通用户的请求.
- cookies是有过期时间的,一旦过期就需要重新获取
6. 超时参数timeout的使用
-
timeout=3表示:发送请求后,3秒钟内返回响应,否则就抛出异常
import requests url = 'https://twitter.com' response = requests.get(url, timeout=3)
7. ip代理和使用verify忽略安全证书
-
实例
proxies = { "http": "http://12.34.56.79:9527", } verify 安全验证忽略方法和ip代理使用方法 response = requests.get(url, proxies=proxies,verify=False)
8. 通过params携带参数字典
-
实例
import requests headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"} # 这是目标url # url = 'https://www.baidu.com/s?wd=python' # 最后有没有问号结果都一样 url = 'https://www.baidu.com/s?' # 请求参数是一个字典 即wd=python kw = {'wd': 'python'} # 带上请求参数发起请求,获取响应 response = requests.get(url, headers=headers, params=kw) print(response.content)
9. cookieJar对象转换为cookies字典的方法
- 转换方法: cookies_dict = requests.utils.dict_from_cookiejar(response.cookies)
- 其中response.cookies返回的就是cookieJar类型的对象
requests.utils.dict_from_cookiejar
函数返回cookies字典
10. requests发送post请求的方法
-
response = requests.post(url, data)
-
data
参数接收一个字典 -
实例
import requests from fake_useragent import UserAgent import json headers = { "User-Agent":UserAgent().random, # 随机浏览器名称 } url = "https://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule" kw = input('请输入要搜索的关键词') data = { 'i': kw, 'from': 'AUTO', 'to': 'AUTO', 'smartresult': 'dict', 'client': 'fanyideskweb', 'salt': '16196147462848', 'sign': '066fea08a845748c34f1781e49117fa9', 'lts': '1619614746284', 'bv': '74d66c0748efe9ba6ee38293a33b65d3', 'doctype': 'json', 'version': '2.1', 'keyfrom': 'fanyi.web', 'action': 'FY_BY_REALTlME', } res = requests.post(url,data=data,headers=headers,verify=False) data = res.json() new_data = data['translateResult'][0][0]['tgt'] print(new_data)
11. 利用requests.session进行状态保持
-
requests.session的作用: 自动处理cookie,即下一次请求会带上前一次的cookie
-
session实例在请求了一个网站后,对方服务器设置在本地的cookie会保存在session中,下一次再使用session请求对方服务器的时候,会带上前一次的cookie
-
实例:
import requests,re headers ={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36', } # 实例化session对象 session = requests.session() # 获取请求所需要的参数 url = 'https://github.com/login' response = session.get(url, headers=headers') # 使用正则匹配获取登录请求需要的参数 authenticity_token = re.search('name="authenticity_token" value="(.*?)" />', response.text).group(1) # 构造登录参数请求 data = { 'commit': 'Sign in', # 固定值 'utf8': '✓', # 固定值 'authenticity_token': authenticity_token, # 该参数在登陆页的响应内容中 'login': input('输入github账号:'), 'password': input('输入github账号:') } # 发送登陆请求(无需关注本次请求的响应) session.post('https://github.com/session', headers=headers, data=data) # 打印需要登陆后才能访问的页面 response = session.get('https://github.com/1596930226', headers=headers) print(response.text)
12.proxy代理参数设置
-
需要根据网站协议的不同 使用相应协议的代理,代理协议有:
- http代理: 目标url为http协议
- https代理: 目标url为https协议
- socks隧道代理
- socks 代理只是简单地传递数据包,不关心是何种应用协议(FTP、HTTP和HTTPS等)
- socks 代理比http、https代理耗时少。
- socks 代理可以转发http和https的请求
-
用法: 字典形式
proxies = { "http": "http://12.34.56.79:9527", "https": "https://12.34.56.79:9527", } response = requests.get(url, proxies=proxies)
13.verify忽略ca证书认证
-
报 ssl.CertificateError … 异常时可以加入
-
实例:
import requests url = '...' response = requests.get(url,verify=False)