新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:pythonrequests重定向的操作
在學(xué)習(xí)requests的有關(guān)內(nèi)容時,細(xì)心的小伙伴會發(fā)現(xiàn)它會自動清理大部分的重定向。本篇就其中的有關(guān)操作帶來介紹。

1、可以使用響應(yīng)對象的 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參數(shù)禁用重定向處理。
>>> 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學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
標(biāo)題名稱:創(chuàng)新互聯(lián)Python教程:pythonrequests重定向的操作
文章位置:http://www.dlmjj.cn/article/djphcod.html


咨詢
建站咨詢
