zoukankan      html  css  js  c++  java
  • python3接口自动化--requests库的使用方法(一)

    一. requests库的介绍与安装

    1.1 介绍

    requests库是一个基于python开发的http库

    1.2 安装

    pip install requests

    二. 请求数据场景

    2.1 请求方式:GET,  请求类型:application/x-www-form-urlencoded

    # -*- coding: utf-8 -*-
    # @Time    : 2021/5/11 23:25
    # @Author  : chinablue
    # @File    : tmp0512.py
    
    import requests
    
    req_data = {'key1': 'value1', 'key2': 'value2'}
    resp = requests.get("http://httpbin.org/get", params=req_data)
    
    print(f"请求地址:{resp.url}")
    print(f"请求方式:{resp.request.method}")
    print(f"请求类型:{resp.request.headers.get('Content-Type')}")  

    执行结果

    请求地址:http://httpbin.org/get?key1=value1&key2=value2
    请求方式:GET
    请求类型:None  #默认的Content-Type为application/x-www-form-urlencoded

    2.2 请求方式:POST,  请求类型:application/x-www-form-urlencoded

    # -*- coding: utf-8 -*-
    # @Time    : 2021/5/11 23:25
    # @Author  : chinablue
    # @File    : tmp0512.py
    
    import requests
    
    req_data = {'key1': 'value1', 'key2': 'value2'}
    resp = requests.post("http://httpbin.org/post", data=req_data)
    
    print(f"请求地址:{resp.url}")
    print(f"请求方式:{resp.request.method}")
    print(f"请求类型:{resp.request.headers.get('Content-Type')}")
    print(f"请求数据:{resp.request.body}")

    执行结果

    请求地址:http://httpbin.org/post
    请求方式:POST
    请求类型:application/x-www-form-urlencoded
    请求数据:key1=value1&key2=value2

    2.3 请求方式:POST,  请求类型:application/json

    # -*- coding: utf-8 -*-
    # @Time    : 2021/5/11 23:25
    # @Author  : chinablue
    # @File    : tmp0512.py
    
    import requests
    
    req_data = {'key1': 'value1', 'key2': 'value2'}
    resp = requests.post("http://httpbin.org/post", json=req_data)
    # resp = requests.put("http://httpbin.org/put", json=req_data)
    # resp = requests.delete("http://httpbin.org/delete", json=req_data)
    
    print(f"请求地址:{resp.url}")
    print(f"请求方式:{resp.request.method}")
    print(f"请求类型:{resp.request.headers.get('Content-Type')}")
    print(f"请求数据:{resp.request.body}")

    执行结果

    请求地址:http://httpbin.org/post
    请求方式:POST
    请求类型:application/json
    请求数据:b'{"key1": "value1", "key2": "value2"}'

    2.4 请求方式:POST,  请求类型:multipart/form-data; boundary=${boundary}

    # -*- coding: utf-8 -*-
    # @Time    : 2021/5/11 23:25
    # @Author  : chinablue
    # @File    : tmp0512.py
    
    import requests
    
    req_data = {'key1': 'value1', 'key2': 'value2'}
    resp = requests.post("http://httpbin.org/post", files=req_data)
    
    print(f"请求地址:{resp.url}")
    print(f"请求方式:{resp.request.method}")
    print(f"请求类型:{resp.request.headers.get('Content-Type')}")
    print(f"请求数据:{resp.request.body}")

    执行结果

    请求地址:http://httpbin.org/post
    请求方式:POST
    请求类型:multipart/form-data; boundary=a59060b098198d3900189d1e51504c4b
    请求数据:b'--a59060b098198d3900189d1e51504c4b
    Content-Disposition: form-data; name="key1"; filename="key1"
    
    value1
    --a59060b098198d3900189d1e51504c4b
    Content-Disposition: form-data; name="key2"; filename="key2"
    
    value2
    --a59060b098198d3900189d1e51504c4b--
    '

    说明事项:

      1. files参数中如果有文件数据,其格式如下:

    files = {
        "file": ("1.png", open("1.png", "rb"), "images/png")
    }

      2. 如果上传的文件比较大,建议使用requests-toolbelt库来辅助

    小结

      1. requests库支持所有的HTTP请求,如get,post,put,delete等

      2. 发送请求时,有4个参数可以传入请求数据:params(参数在url上),data,json,files

    三. 响应数据场景

    # -*- coding: utf-8 -*-
    # @Time    : 2021/5/11 23:25
    # @Author  : chinablue
    # @File    : tmp0512.py
    
    import requests
    
    resp = requests.get("http://httpbin.org/get")
    
    print(f"响应状态码: {resp.status_code}")
    print(f"响应头信息: {resp.headers}")
    print(f"响应cookies信息: {resp.cookies}")
    print(f"获取PreparedRequest对象:{resp.request}")
    print(f"响应内容(str类型): {resp.text}")
    print(f"响应内容(dict类型): {resp.json()}")  #如果响应类型不是json,则抛出异常
    print(f"响应内容(bytes类型): {resp.content}")

    说明事项:

      1. resp.cookies返回的是一个RequestsCookieJar对象,若将其转为字典对象:

    # -*- coding: utf-8 -*-
    # @Time    : 2021/5/11 23:25
    # @Author  : chinablue
    # @File    : tmp0512.py
    
    import requests
    
    resp = requests.get("https://www.baidu.com/img/flexible/logo/pc/result.png")
    
    print(f"响应cookies信息(字典类型): {requests.utils.dict_from_cookiejar(resp.cookies)}")
    print(f"响应cookies信息(字典类型): {resp.cookies.get_dict()}")
    print(f"响应cookies信息(列表类型): {resp.cookies.items()}")

      2. resp.request返回的是一个PreparedRequest对象,可以从该对象中获取到相关请求信息

    print(f"请求地址:{resp.request.url}")
    print(f"请求方法:{resp.request.method}")
    print(f"请求头信息:{resp.request.headers}")

      3. resp.content返回的是二进制数据,一般用于获取图片或文件数据

    # -*- coding: utf-8 -*-
    # @Time    : 2021/5/11 23:25
    # @Author  : chinablue
    # @File    : tmp0512.py
    
    import requests
    
    resp = requests.get("https://www.baidu.com/img/flexible/logo/pc/result.png")
    
    if resp.status_code == requests.codes.ok:
        with open("baidu.png", "wb") as f:
            f.write(resp.content)
    else:
        raise Exception(f"请求失败,当前http响应码: {resp.status_code}")
  • 相关阅读:
    poj1966 Cable TV Network
    contesthunter#17-c 舞动的夜晚
    joyoi1957 「Poetize5」Vani和Cl2捉迷藏
    joyoi1935 「Poetize3」导弹防御塔
    luogu3629 [APIO2010]巡逻
    poj2728 Desert King
    poj1734 Sightseeing trip
    loj2003 「SDOI2017」新生舞会
    hdu2255 奔小康赚大钱 KM 算法
    POJ 1681 Painter's Problem(高斯消元+枚举自由变元)
  • 原文地址:https://www.cnblogs.com/reconova-56/p/14761771.html
Copyright © 2011-2022 走看看