requests爬虫详解

Requests

安装

复制代码
pip install requests

示例

复制代码
from fake_useragent import UserAgent
import requests


def cra1_1():
url = 'http://xx/front/website/findAllTypes'
headers = {'User-Agent': UserAgent().chrome}
resp = requests.get(url, headers=headers)
result = resp.json()


if __name__ == '__main__':
cra1_1()          

发送请求

GET请求

复制代码
resp = requests.get(url,headers= headers,params=params1) #headers,params1是字典

POST请求

复制代码
resp = requests.post(url,headers=headers,data=data) #headers,data是字典

获取响应信息

获取响应信息

resp.status_code 获取状态码

resp.text 获取响应内容 (以字符串)

resp.json() 获取响应内容【python数据,可直接用jsonpath解析】

resp.content 获取响应内容(以字节的方式)

resp.headers 获取响应头内容

resp.url 获取访问地址

resp.encoding 获取网页编码

resp.request.headers 请求头内容

resp.cookie 获取cookie

功能

代理访问

复制代码
proxies = {"http": "http://10.10.1.10:3128","https": "https://10.10.1.10:1080",}
requests.get("http://www.zhidaow.com", proxies=proxies)

设置超时时间

复制代码
requests.get('http://github.com', timeout=0.001)

session自动保存cookies

复制代码
s = requests.Session() # 创建一个session对象
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789') # 用session对象发出get请求,设置cookies

ssl验证

复制代码
requests.packages.urllib3.disable_warnings() # 禁用安全请求警告
resp = requests.get(url, verify=False, headers=headers)
相关推荐
SelectDB1 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
荣码9 小时前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
金銀銅鐵20 小时前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
Lyn_Li21 小时前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸1 天前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学1 天前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田2 天前
Pydantic校验配置文件
python
hboot2 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi2 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi2 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab