美图齐众专注资阳网站设计 资阳网站制作 资阳网站建设
资阳网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

创新互联Python教程:pythonrequests重定向的操作

在学习requests的有关内容时,细心的小伙伴会发现它会自动清理大部分的重定向。本篇就其中的有关操作带来介绍。

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

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

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

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
[]

以上就是python requests重定向操作的介绍,希望对大家有所帮助。更多Python学习指路:创新互联python教程

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


网页名称:创新互联Python教程:pythonrequests重定向的操作
浏览路径:http://www.zsjierui.cn/article/djphcod.html

其他资讯