Python.Requests库的基本使用
Requests安装#
使用pip安装命令:
pip install requests
打开cmd,输入python然后导入requests如果安装成功没有任何提示
如果提示如下则说明安装失败
ImportError: No module named 'requests'
Requests 基础应用#
发送不同类型HTTP请求#
requests库内置了不同的方法来发送不同类型的http请求,用法如下所示:
import requests base_url = "http://httpbin.org" # 发生GET类型请求 r_get = requests.get(base_url + "/get") print(r_get.status_code) # 发生POST类型请求 r_post = requests.post(base_url + "/post") print(r_post.status_code) # 发生PUT类型请求 r_put = requests.put(base_url + "/put") print(r_put.status_code) # 发生DELETE类型请求 r_delete = requests.delete(base_url + "/delete") print(r_delete.status_code)
执行结果,200是状态码表示发送请求成功
200 200 200 200
参数传递
传递URL参数
一般在GET请i去中使用字符串(query string)来进行参数传递,在requests库中使用方法如下:
param_data = {'hero': 'leesin'} r_data = requests.get(base_url + '/get', params=param_data) print(r_data.url) print(r_data.status_code)
执行结果
http://httpbin.org/get?hero=leesin
200
传递body参数#
在post请求中,一般参数都在请求体(Request body)中传递,在Requests中用法如下:
form_data = {'hero': 'leesin'} r_body = requests.post(base_url + '/post', data=form_data) print(r_body.text)
执行结果
{ "args": {}, "data": "", "files": {}, "form": { "hero": "leesin" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "11", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.22.0" }, "json": null, "origin": "61.144.173.21, 61.144.173.21", "url": "https://httpbin.org/post" }
请求头定制#
如果你想为请求添加HTTP头部,只要简单传递一个dict给headers参数就可以了。
用法如下:
form_data = {'hero': 'leesin'} header = {'user-agent': 'Mozilla/5.0'} r_headers = requests.post(base_url + '/post', data=form_data, headers=header) print(r_headers.text)
返回值
{ "args": {}, "data": "", "files": {}, "form": { "hero": "leesin" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "11", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0" }, "json": null, "origin": "61.144.173.21, 61.144.173.21", "url": "https://httpbin.org/post" }
响应内容#
当请求发送成功之后,我们可以获取响应内容。如响应状态码,响应头信息,响应体内容
form_data = {'hero': 'leesin'} header = {'user-agent': 'Mozilla/5.0'} r_response = requests.post(base_url + '/post', data=form_data, headers=header) # 获取响应状态码 print(r_response.status_code) # 获取响应头信息 print(r_response.headers) # 获取响应内容 print(r_response.text) # 将响应内容以json格式返回 print(r_response.json())
返回值
200 {'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Origin': '*', 'Content-Encoding': 'gzip', 'Content-Type': 'application/json', 'Date': 'Fri, 28 Jun 2019 14:38:09 GMT', 'Referrer-Policy': 'no-referrer-when-downgrade', 'Server': 'nginx', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'DENY', 'X-XSS-Protection': '1; mode=block', 'Content-Length': '258', 'Connection': 'keep-alive'} { "args": {}, "data": "", "files": {}, "form": { "hero": "leesin" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "11", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0" }, "json": null, "origin": "61.144.173.21, 61.144.173.21", "url": "https://httpbin.org/post" } {'args': {}, 'data': '', 'files': {}, 'form': {'hero': 'leesin'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '11', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'Mozilla/5.0'}, 'json': None, 'origin': '61.144.173.21, 61.144.173.21', 'url': 'https://httpbin.org/post'}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南