requests模块的使用
2021-08-19 22:20
771 查看
一、requests的使用
安装:pip install requests
get请求
1、发送 get 请求
import requests header = { 'referer': 'https://www.baidu.com' } # 请求并获取返回结果 re = requests.get('https://t7.baidu.com/it/u=1951548898,3927145&fm=193', headers=header)
2、请求中携带数据
# 第一种:中文会被编码 ret = requests.get('https://www.baidu.com/s?wd=小杨') # 第二种:会自动进行编码(推荐) ret = requests.get('https://www.baidu.com/', params={'wd': '小杨'})
3.请求中携带cookie
# 方式一:在header中放入cookie header = { 'referer': 'https://www.baidu.com', 'cookie':'key=qeinpkdi;key2=laisdno;key3=winqcl' } ret = requests.get('http://127.0.0.1', headers=header) # 方式二:cookies是一个字典或者CookieJar对象 ret = requests.get('http://127.0.0.1', cookies={'key': 'insidns'})
post请求
post 请求和 get 请求大致都是一样的:
1、发送post请求,携带数据
ret = requests.post('http://127.0.0.1', data={'name': '小杨'})
2、自动携带cookie
session = requests.session() res = session.post('http://127.0.0.1/login/') # 假设这个请求登录了 ret = session.get('http://127.0.0.1/index/') # 现在不需要手动带cookie,session会自己处理
response响应对象
也就是请求后响应的对象
1、查看响应信息
response=requests.post('http://127.0.0.1:8000/index/',data={'name':'xiaoyang'}) print(response.text) # 响应的文本 print(response.content) # 响应体的二进制 print(response.status_code) # 响应状态码 print(response.headers) # 响应头 print(response.cookies) # cookie print(response.cookies.get_dict()) # 把cookie转成字典 print(response.cookies.items()) # key和value print(response.url) # 请求的url print(response.history) # []放重定向之前的地址 print(response.encoding) # 响应的编码方式 response.iter_content() # 图片,视频,大文件,可以以一点一点循环取出来 # 例如: with open('a.jpg', 'wb') as f: for line in response.iter_content(): f.write(line)
2、编码问题
ret = requests.get('http://127.0.0.1') # 如果打印出来的是乱码 # 方式一:可以从HTML标签meta中查看在encoding ret.encoding='gb2312' # 方式二:自动去HTML标签中查,不用自己去查 ret.encoding=ret.apparent_encoding
3、解析 JSON
ret = requests.get('http://127.0.0.1') # 方式一:自己解析的情况下 import json json.loads(ret.text) # 方式二: ret.json()
高级用法
1、SSL证书验证
https 的请求,会先检查证书是否合法,不合法就报错。
# 去掉报错,但是会报警告 ret = requests.post('https://127.0.0.1', verify=False) # 不验证证书,报警告,返回200 # 去掉报错,并且去掉报警信息 from requests.packages import urllib3 urllib3.disable_warnings() # 关闭警告 respone=requests.get('https://127.0.0.1', verify=False) # 使用证书,需要手动携带 ret = requests.post('https://127.0.0.1', cert=('/path/server.crt', '/path/key' ) )
2、使用代理
# 格式: respone=requests.get('http://127.0.0.1:8000/index/',proxies={'http':'代理的地址和端口号',}) # 代理池:列表放了一堆代理ip,每次随机取一个,再发请求就不会封ip了 # 如果使用高匿代理,后端无论如何拿不到你的ip,使用透明,后端能够拿到你的ip # 后端可以通过 X-Forwarded-For 拿到透明代理的ip。 respone=requests.get('https://www.baidu.com/',proxies={'http':'27.46.20.226:8888',})
3、超时设置
respone=requests.get('https://www.baidu.com', timeout=0.0001)
4、文件上传
res=requests.post('http://127.0.0.1',files={'myfile':open('a.jpg','rb')})
相关文章推荐
- python使用requests模块实现爬取电影天堂最新电影信息
- Python 使用requests模块发送GET和POST请求的实现代码
- 爬虫实例——主要使用re和requests模块
- 记录使用requests模块实现登陆csdn
- [Trouble Shooting] 解决requests模块使用过程中遇到 certificate verify failed 错误
- requests模块 线程池 协程使用
- Python3使用requests模块实现显示下载进度的方法详解
- requests模块的基本使用
- 爬虫学习笔记004-requests模块使用代理
- Python3使用requests模块显示下载进度
- 使用requests模块发送带有参数的请求
- python中如何使用requests模块下载文件并获取进度提示?
- Python使用requests模块访问HTTPS网站报错`certificate verify failed`
- requests模块的入门和基本使用
- 使用monkey技术修改python requests模块
- python使用requests模块模拟登陆知乎
- python requests模块的安装和使用方法
- python中requests模块的使用方法
- Python3使用requests模块显示下载进度
- Python3使用requests模块显示下载进度