美文网首页
requests.request()方法解析

requests.request()方法解析

作者: 测试在路上 | 来源:发表于2020-10-29 12:54 被阅读0次

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

    (1)request参数说明

    • method: 支持 GET, OPTIONS, HEAD, POST, PUT, PATCH, 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)
    

    image
    • 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) 毫秒

    相关文章

      网友评论

          本文标题:requests.request()方法解析

          本文链接:https://www.haomeiwen.com/subject/onukvktx.html