requests.request()方法解析

2 篇文章 0 订阅
1 篇文章 0 订阅

别问 requests.request () 有多强大,问就是🐂byte

(1)request参数说明

  • method: 支持 GETOPTIONSHEADPOSTPUTPATCH, or DELETE.
  • url: str类型
  • params: (可选) Dict, list of tuples or bytes to send.
params={'q': 'python', 'cat': '1001'}
  • data: (可选) Dictionary, list of tuples, bytes, or file-like
    • requests默认使用application/x-www-form-urlencoded对POST数据编码
data={'form_email': 'abc@example.com', 'form_password': '123456'}
  • json: (可选) 如果要传递JSON数据,可以直接传入json参数:
params = {'key': 'value'}
requests.request(method="post", url="", json=params) # 内部自动序列化为JSON
  • headers: (可选) dict
  • cookies: (可选) dict
cs = {'token': '12345', 'status': 'working'}
requests.request(method="get", url="", cookies=cs)
  • files: (可选) 上传文件需要更复杂的编码格式,但是requests把它简化成files参数
    • 在读取文件时,注意务必使用'rb'即二进制模式读取,这样获取的bytes长度才是文件的长度
upload_files = {'file': open('report.xls', 'rb')}
requests.request(method="post", url="", files=upload_files)
  • auth: (可选) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
  • timeout: (可选) 访问超时, float(wait for server to send data ) or tuple(connect timeout, read timeout)
  • allow_redirects: (可选) 重定向:Boolean. 默认为true。 Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection.
  • proxies: (可选) 设置代理,可抓取所有http和https请求
proxies = {
  'http': 'http://10.100.57.47:8001',
  'https': 'http://10.100.57.47:8001',
}
requests.get('https://testerhome.com/', proxies=proxies)

 

  • verify: (可选) Boolean,控制是否验证,默认为True。当verify为True时, 如果想解析https内容,需在Cert参数中添加证书路径
  • stream: (可选) 如果为``False'',则将立即下载响应内容。
  • cert: (可选) string 或 元组
    • string:为ssl客户端证书文件(.pem)的路径
    • 元组:(“证书”,“密钥”)配对

(2)response参数说明

  • 返回状态码:r.status_code
  • Response Body:
    • str: r.text
    • Bytes: r.content
    • Dict: r.json()
  • Response Header(Dict): r.headers
  • 自动检测编码:r.encoding
  • 响应时间: int(r.elapsed.microseconds/1000) 毫秒
  • 14
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值