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)
相关推荐
虾球xz3 分钟前
CppCon 2018 学习:THE MOST VALUABLE VALUES
开发语言·c++·学习
赶紧去巡山19 分钟前
pyhton基础【23】面向对象进阶四
python
旷世奇才李先生41 分钟前
PyCharm 安装使用教程
ide·python·pycharm
阿蒙Amon1 小时前
C#扩展方法全解析:给现有类型插上翅膀的魔法
开发语言·c#
这里有鱼汤1 小时前
“对象”?对象你个头!——Python世界观彻底崩塌的一天
后端·python
尘浮7281 小时前
60天python训练计划----day59
开发语言·python
wh39331 小时前
使用Python将PDF转换成word、PPT
python·pdf·word
船长@Quant2 小时前
数学视频动画引擎Python库 -- Manim Voiceover 语音服务 Speech Services
python·数学·manim·动画引擎·语音旁白
Chef_Chen2 小时前
从0开始学习R语言--Day39--Spearman 秩相关
开发语言·学习·r语言
不学会Ⅳ2 小时前
Mac M芯片搭建jdk源码环境(jdk24)
java·开发语言·macos