[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)
相关推荐
鲁邦通物联网10 分钟前
DNV新造船海事网关安全通信架构与系列船验证方法
运维·安全·海事网关·船用网关·海事网络安全·智慧航运·船用路由器
BK贝壳5 小时前
bool-hybrid-array 的 GPU_opt 与 cjx:一个没显卡就报错的开关,一个把 800 字压到 96 字节的编码
python
考虑考虑9 小时前
tcpdump指令
运维·后端·自动化运维
BD_Marathon9 小时前
安装Jupyter notebook
ide·python·jupyter
Zlzxzw9 小时前
End-to-End Autonomous Generation of Human Assembly Plans
python
Wellijohn10 小时前
2000 个请求、30% 失败率:我把 Python 批量并发的限流和重试参数算了一遍
后端·python·性能优化
feixing_fx11 小时前
解构与展开:可读性和性能的取舍
开发语言·前端·javascript·前端框架
无奈笑天下11 小时前
Linux上kdump故障标准处理流程
linux·运维·服务器·github
qyr678911 小时前
全球汽车和航空航天用磁铁材料市场竞争格局变化趋势
大数据·人工智能·物联网·自动化·汽车和航空航天用磁铁材料
Logintern0912 小时前
deep agents 多智能体模式——规划与委派
python·langchain