API 接口自动化测试详细图文教程学习系列10--Requests模块2--举例说明

测试学习记录,仅供参考!

Requests 模块

Python requests库是一个常用的 Python HTTP客户端请求库,是一个简单易用、功能强大的库,特别适合用来与 RESTful API 进行交互,用于发送各种HTTP请求,如GET、POST、PUT、DELETE等,并处理返回的响应。使用 requests 库,开发者可以轻松地添加头部信息、发送表单数据、上传文件、管理会话、处理 Cookies 、使用代理等;它还可以支持 SSL 证书验证、连接超时设置、会话对象复用等高级功能。

三、Requests 模块发起接口请求

Requests 的基本使用

requests 的基本使用可以简单分为以下三个步骤;

  1. 导入模块,首先需要引入requests 模块;

  2. 其次是发送 http 请求(例如:get 请求),获取响应;

  3. 最后从响应信息中获取数据;

    引入相关模块,导入请求库

    import requests

    设置请求网址,发起HTTP请求,发送一个get请求,并把它赋值给变量response

    response = requests.get('https://www.baidu.com/')

    获取打印响应数据

    print(response.text)

    打印输出结果

    ç™¾åº¦ä¸€ä¸‹ï¼Œä½ å°±çŸ¥é“

    b'\r\n <meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge>\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93

    \r\n'
    200
    {'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Content-Encoding': 'gzip', 'Content-Length': '1108', 'Content-Type': 'text/html', 'Pragma': 'no-cache', 'Server': 'bfe', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Date': 'Tue, 07 Apr 2026 20:05:19 GMT'}
    <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
    {'BDORZ': '27315'}
    [('BDORZ', '27315')]
    http://www.baidu.com/
    []
    ISO-8859-1

    进程已结束,退出代码为 0

  4. 其他
    复制代码
    import requests
    respone=requests.get('http://www.jianshu.com')
    # respone属性
    print(respone.text)
    print(respone.content)
    print(respone.status_code)
    print(respone.headers)
    print(respone.cookies)
    print(respone.cookies.get_dict())
    print(respone.cookies.items())
    print(respone.url)
    print(respone.history)
    print(respone.encoding)
    #关闭:response.close()
    from contextlib import closing
    with closing(requests.get('http://www.jianshu.com',stream=True)) as response:
        for line in response.iter_content():
            pass
    
    <html>
    <head><title>403 Forbidden</title></head>
    <body>
    <center><h1>403 Forbidden</h1></center>
    <hr><center>openresty</center>
    </body>
    </html>
    
    b'<html>\r\n<head><title>403 Forbidden</title></head>\r\n<body>\r\n<center><h1>403 Forbidden</h1></center>\r\n<hr><center>openresty</center>\r\n</body>\r\n</html>\r\n'
    403
    {'Date': 'Tue, 07 Apr 2026 20:18:12 GMT', 'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'Content-Encoding': 'gzip'}
    <RequestsCookieJar[]>
    {}
    []
    https://www.jianshu.com/
    [<Response [302]>]
    ISO-8859-1
    
    进程已结束,退出代码为 0

    其他烦请自行学习了解。

    复制代码
    # 把二进制数据转换字符串
    print(response.content.decode())
    # 在方法内部判断resp.status_code是否等于200,如果不等于,则抛出异常
    response.raise_for_status() 	
    # 从内容中分析出的响应内容编码方式(备选编码方式)
    response.apparent_encoding    
    # 得到对应的 json 格式的数据,类似于字典
    response.json()	

    未完待续。。。

相关推荐
兵慌码乱11 小时前
面向桌面端的资产管理系统分层架构设计与核心模块实现
python·系统架构·sqlite·pyqt5·数据库设计·桌面应用开发·mvc架构
hboot13 小时前
AI工程师第三课 - 机器学习基础
python·scikit-learn·kaggle
顾林海17 小时前
Agent入门阶段-编程基础-Python:流程控制
python·agent·ai编程
呱呱复呱呱20 小时前
Django CBV 源码解读:一个请求是怎么找到你的 get() 方法的
python·django
曲幽1 天前
刚部署的 LibreTranslate 频频翻车?我掏出了 20 年前的 StarDict 词典,用 FastAPI 搭了个本地词典翻译 API
python·fastapi·web·translate·goldendict·libretranslate·stardict·pystardict
荣码1 天前
用Streamlit给AI应用套个界面,10行代码出Web页面
java·python
兵慌码乱1 天前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
金銀銅鐵2 天前
[Python] 体验用欧几里得算法计算最大公约数的过程
python·数学
FreakStudio2 天前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
用户0332126663672 天前
使用 Python 从零创建 Word 文档
python