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)
相关推荐
databook20 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室21 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三1 天前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户2519162427111 天前
Python之语言特点
python
刘立军1 天前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机1 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i1 天前
django中的FBV 和 CBV
python·django
c8i1 天前
python中的闭包和装饰器
python
这里有鱼汤1 天前
小白必看:QMT里的miniQMT入门教程
后端·python