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()	

    未完待续。。。

相关推荐
2601_967987371 分钟前
使用 NumPy 和 Python 实现梯度下降
python·机器学习·线性回归·numpy·梯度下降
BD_Marathon40 分钟前
Jupyter魔法命令
ide·python·jupyter
词却1 小时前
Python 正则表达式实战:使用 re 模块提取符合条件的字符串
python·正则表达式
汤姆宝库2 小时前
视觉语言模型的实现:LLaVA式投影与指令微调
python·深度学习·语言模型
2601_9679791212 小时前
初学者必知的Python中优雅的用法
python·枚举·初学者·优雅用法·字典解析
AI创界者13 小时前
Python 进阶实战(五十八章):配置热更新、优雅停机排空与高并发压测方案
人工智能·python·aigc
半扇排骨15 小时前
特别篇:在线 XSS 靶场 haozi 分享与通关笔记
学习·安全·靶场·xss
2601_9679873717 小时前
python网络工程师自动化运维课程
python·正则表达式·网络工程师·模块·自动化运维
吴凡wudianmei17 小时前
给 LLM 套输出契约:校验失败怎么把错误回灌给模型
python·llm·agent
祺曦18 小时前
复现连续手语识别项目
python·手语识别·复现项目·corrnet