关键词搜索

源码搜索 ×
×

requests模块的使用

发布2021-08-20浏览367次

详情内容

一、requests的使用

安装:pip install requests

get请求

1、发送 get 请求

  1. import requests
  2. header = {
  3. 'referer': 'https://www.baidu.com'
  4. }
  5. # 请求并获取返回结果
  6. re = requests.get('https://t7.baidu.com/it/u=1951548898,3927145&fm=193', headers=header)

2、请求中携带数据

  1. # 第一种:中文会被编码
  2. ret = requests.get('https://www.baidu.com/s?wd=小杨')
  3. # 第二种:会自动进行编码(推荐)
  4. ret = requests.get('https://www.baidu.com/', params={'wd': '小杨'})

3.请求中携带cookie

  1. # 方式一:在header中放入cookie
  2. header = {
  3. 'referer': 'https://www.baidu.com',
  4. 'cookie':'key=qeinpkdi;key2=laisdno;key3=winqcl'
  5. }
  6. ret = requests.get('http://127.0.0.1', headers=header)
  7. # 方式二:cookies是一个字典或者CookieJar对象
  8. 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

  1. session = requests.session()
  2. res = session.post('http://127.0.0.1/login/') # 假设这个请求登录了
  3. ret = session.get('http://127.0.0.1/index/') # 现在不需要手动带cookie,session会自己处理

response响应对象

也就是请求后响应的对象

1、查看响应信息

  1. response=requests.post('http://127.0.0.1:8000/index/',data={'name':'xiaoyang'})
  2. print(response.text) # 响应的文本
  3. print(response.content) # 响应体的二进制
  4. print(response.status_code) # 响应状态码
  5. print(response.headers) # 响应头
  6. print(response.cookies) # cookie
  7. print(response.cookies.get_dict()) # 把cookie转成字典
  8. print(response.cookies.items()) # keyvalue
  9. print(response.url) # 请求的url
  10. print(response.history) # []放重定向之前的地址
  11. print(response.encoding) # 响应的编码方式
  12. response.iter_content() # 图片,视频,大文件,可以以一点一点循环取出来
  13. # 例如:
  14. with open('a.jpg', 'wb') as f:
  15. for line in response.iter_content():
  16. f.write(line)

2、编码问题

  1. ret = requests.get('http://127.0.0.1')
  2. # 如果打印出来的是乱码
  3. # 方式一:可以从HTML标签meta中查看在encoding
  4. ret.encoding='gb2312'
  5. # 方式二:自动去HTML标签中查,不用自己去查
  6. ret.encoding=ret.apparent_encoding

3、解析 JSON

  1. ret = requests.get('http://127.0.0.1')
  2. # 方式一:自己解析的情况下
  3. import json
  4. json.loads(ret.text)
  5. # 方式二:
  6. ret.json()

高级用法

1、SSL证书验证

https 的请求,会先检查证书是否合法,不合法就报错。

  1. # 去掉报错,但是会报警告
  2. ret = requests.post('https://127.0.0.1', verify=False) # 不验证证书,报警告,返回200
  3. # 去掉报错,并且去掉报警信息
  4. from requests.packages import urllib3
  5. urllib3.disable_warnings() # 关闭警告
  6. respone=requests.get('https://127.0.0.1', verify=False)
  7. # 使用证书,需要手动携带
  8. ret = requests.post('https://127.0.0.1',
  9. cert=('/path/server.crt',
  10. '/path/key'
  11. )
  12. )

2、使用代理

  1. # 格式:
  2. respone=requests.get('http://127.0.0.1:8000/index/',proxies={'http':'代理的地址和端口号',})
  3. # 代理池:列表放了一堆代理ip,每次随机取一个,再发请求就不会封ip了
  4. # 如果使用高匿代理,后端无论如何拿不到你的ip,使用透明,后端能够拿到你的ip
  5. # 后端可以通过 X-Forwarded-For 拿到透明代理的ip。
  6. respone=requests.get('https://www.baidu.com/',proxies={'http':'27.46.20.226:8888',})

3、超时设置

  1. respone=requests.get('https://www.baidu.com',
  2. timeout=0.0001)

4、文件上传

res=requests.post('http://127.0.0.1',files={'myfile':open('a.jpg','rb')})

学习python教程之旅

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载