Python模块之 requests - 请求HTTP库

requests - 请求HTTP库

 

常用网络请求速查表:https://www.cnblogs.com/wutou/p/15802687.html

来源:https://www.52pojie.cn/thread-1551640-1-1.html

TIF格式,原图下载:https://wwa.lanzoui.com/iXVLFwx2uwj

 

好文推荐: https://www.cnblogs.com/lanyinhao/p/9634742.html

 

作用:

   请求HTTP库

必要操作:

>>> import requests

帮助查看:

>>> help(requests)

或 单独查看某个方法(函数)

>>> help(requests.get)
>>> help(requests.encoding)

  

方法(函数):

  ## 请求https://baidu.com页面

r = requests.get('https://baidu.com')
  ## 查看当前编码
r.encoding

  ## 设置编码utf-8 ,因为网页是utf-8,编码不对显示汉字会乱码

r.encondig = 'utf-8'

  ## 确保程序在下载失败时停止[1]

r.raise_for_status() 

  ## 获取文件长度(字节数)[2]    获取来自服务器的原始套接字响应stream=True

>>> url = 'https://wppkg.baidupcs.com/issue/netdisk/gray/1.4.2/202112051127/tv_1.4.2.apk'
>>> r = requests.get(url, stream=True)
>>> r.headers.get('content-length')
'32188097'

  ## request的函数(可做for的迭代对象);chunk_size = 1024的块大小可以按实际情况写

r.iter_content(chunk_size=1024)

  ## 打印返回信息(response)

print(r.)          # 打印请求方法
print(r.url)             # 打印请求URL
print(r.headers)         # 打印请求头
print(r.body)            # 打印请求体

  

  ## 打印请求信息

response = requests.get('http://www.example.com')
request = response.request
print(request.method)          # 打印请求方法
print(request.url)             # 打印请求URL
print(request.headers)         # 打印请求头
print(request.body)            # 打印请求体

  

  #python的requests模块进行下载带宽限制,进行现在速度限制,避免拉爆服务器。
  #开启requests的stream=True就可以进行渐进式下载,然后再适当的sleep一下。
  #就可以减少下载带宽,限制下载速度了。

requests.get(http_url,stream=True)

  

  # 随机User-Agent

from fake_useragent import UserAgent
ua = UserAgent()
headers = {
  #ua.random 表示的时 随机生成一个User-Agent,这样的话我们就能有很多个 User-Agent 来使用,
  #就不用再担心 被封ip了。
  "User-Agent": ua.random,
  "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
  "Accept-Language" : "zh-CN,zh;q=0.9",
  "Accept-Encoding" : "gzip, deflate, br",
  "DNT" : "1",
  "Connection" : "cloes"
}

  

 

 

参数:

requests.get()方法所有参数顺序

url(必选)、params、allow_redirects、auth、cert、cookies、headers、proxies、stream、timeout、verify

参数 状态 各参数的描述
url  必须 请求的网址
params  可选 字典,要作为查询字符串发送的元组或字节的列表。默认None
allow_redirects  可选 用于启用/禁用重定向的布尔值。默认True(允许重定向)
auth  可选 用于启用某种HTTP身份验证的元组。默认None
cert  可选 指定证书文件或密钥的字符串或元组。默认None
cookies  可选 发送到指定网址的Cookie字典。默认None
headers  可选 发送到指定网址的HTTP标头字典。默认None
proxies  可选 URL代理协议字典。默认None
stream  可选 如果响应应立即下载(False)或流式传输(限制下载速度了 True)的布尔指示。默认False
timeout  可选 一个数字或一个元组,指示等待客户端建立连接和/或发送响应的秒数。 默认值None表示请求将继续,直到连接关闭
verify  可选 用于验证服务器TLS证书的布尔值或字符串指示。默认True

 

requests.post()方法所有参数顺序:url(必选)、data、json、files、allow_redirects、auth、cert、cookies、headers、proxies、stream、timeout、verify

参数  状态   各参数的描述
 url  必须   请求的网址
 data  可选  元组列表,字节或要发送到指定URL的文件对象
 json  可选  要发送到指定URL的JSON对象
 files  可选  要发送到指定URL的文件字典
 allow_redirects  可选  用于启用/禁用重定向的布尔值。默认True(允许重定向)
 auth  可选  用于启用某种HTTP身份验证的元组。默认None
 cert  可选  指定证书文件或密钥的字符串或元组。默认None
 cookies  可选  要发送到指定网址的Cookie字典。 默认None
 headers  可选  要发送到指定网址的HTTP标头字典。默认None
 proxies  可选  URL代理协议字典。默认None
 stream  可选  如果响应应立即下载(False)或流式传输(限制下载速度了 True)的布尔指示。默认False
timeout  可选 一个数字或一个元组,指示等待客户端建立连接和/或发送响应的秒数。默认值 None表示请求将继续,直到连接关闭
verify  可选 用于验证服务器TLS证书的布尔值或字符串指示。默认True

 

 

 

 

 

 

 


参考、说明:

^[1] https://blog.csdn.net/weixin_41848989/article/details/88554700

^[2] https://blog.csdn.net/weixin_36896856/article/details/108016558 

 https://deepinout.com/python/python-qa/49_python_python_requests_print_entire_http_request_raw.html

 https://www.cnblogs.com/zpf1092841490/p/18002287 (参数说明)

 https://blog.csdn.net/qq_41945828/article/details/105173061 (随机User-Agent、限制下载速度了)

 

posted @ 2022-01-14 16:40  悟透  阅读(104)  评论(0编辑  收藏  举报