requests模块的get和post请求
中文文档:添加链接描述
1. 安装命令:pip install requests
2. requests的—get请求
# 导入requests模块
import requests
# 准备要访问的ur
url = f"https://www.sogou.com/web?query=搜索内容"
# 设置请求头
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
}
# 发送get请求
resp = requests.get(url, headers=headers)
# 返回内容
print(resp.text)
3…requests的post请求
# 开发时间: 2022/9/13 17:14
# ajax异步 获取数据 使用post请求
import requests
url = "https://fanyi.baidu.com/sug"
# 设置post内容
dat = {
"kw": "搜索内容"
}
resp = requests.post(url, data=dat)
print(resp.json())
4.返回内容
`