当前位置: 首页 > 图灵资讯 > 行业资讯> python requests重定向的操作

python requests重定向的操作

发布时间:2024-06-18 09:39:15

在学习requests的相关内容时,细心的朋友会发现它会自动清理大部分重定向。本文介绍了相关操作。

1、响应对象可以使用 history 跟踪重定向的方法。

Response.history 是一个:class:Response<requests.Response> 这些对象的列表是为了完成要求而创建的。

>>>r=requests.get('http://github.com')
>>>r.url
'https://github.com/'
>>>r.status_code
200
>>>r.history
[<Response[301]>]

2、重定向处理被禁用于allow_redirects参数。

>>>r=requests.get('http://github.com',allow_redirects=False)
>>>r.status_code
301
>>>r.history
[]

3、若使用HEAD,则可启用重定向。

>>>r=requests.head('http://github.com',allow_redirects=True)
>>>r.url
'https://github.com/'
>>>r.history
[<Response[301]>]

以上是python 介绍requests重定向操作,希望对大家有所帮助。更多Python学习指导:python基础教程

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09