[Web自动化] Requests模块基本使用

6.1 Requests模块基本使用

requests 是一个用 Python 编写的 HTTP 库,它提供了简单易用的 API 来发送 HTTP/1.1 请求。由于它非常直观且功能强大,因此被广大 Python 开发者广泛使用于网络爬虫、API 调用等场景。

6.1.1 模块的安装与导入

py 复制代码
pip install requests  # 安装
import requests  # 导入

6.1.2 发送请求

py 复制代码
import requests
# 最基本的不带参数的get请求
r1 = requests.get('https://www.baidu.com') 
# 带参数的get请求(相当于访问:http://dict.baidu.com/s?wd=python)
r2 = requests.get(url='http://dict.baidu.com/s', params={'wd': 'python'})

6.1.3 其它请求

py 复制代码
import requests
import json
requests.get('https://github.com/timeline.json')# GET请求
requests.post('http://httpbin.org/post')# POST请求
requests.put('http://httpbin.org/put')# PUT请求
requests.delete('http://httpbin.org/delete')# DELETE请求
requests.head('http://httpbin.org/get')# HEAD请求
requests.options('http://httpbin.org/get')# OPTIONS请求
r1 = requests.post('http://httpbin.org/post', data={'key': 'value'})
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
r2 = requests.post(url=url, data=json.dumps(payload))
r3 = requests.post(url=url, json=payload)
相关推荐
Gigavision32 分钟前
基于BUAA-MIHR数据集的噪声解耦对比学习算法
人工智能·python·深度学习·算法
IPdodo_1 小时前
跨境 API 调用不稳定怎么办:出口、超时重试与链路监控的实践
网络·python·网络协议
Madison-No71 小时前
搭建项目测试环境
linux·运维·服务器·python
+VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue蛋糕店管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·课程设计
计算机编程-吉哥2 小时前
YOLO26 vs YOLO11 vs YOLOv8:深度学习咖啡果实成熟度分割系统【计算机毕业设计选题推荐】
人工智能·python·深度学习·yolo·django·毕业设计
吴声子夜歌2 小时前
Shell编程实例——编写安全的shell脚本(二)
linux·运维·shell
大衛說2 小时前
11 · 异常处理与日志
python
悠悠121383 小时前
微服务通信别只看性能:REST 与 gRPC 的 6 个选型判断和一次 502 故障复盘
运维·服务器
auto_go3 小时前
Python 实战指南(7)——账本动不动就崩?先把“魔法字符串”和“裸报错”干掉
python
Beyond_System|系统之外3 小时前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法