python requests重定向的操作
在学习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基础教程
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。