how do python capture 302 redirect url

依然范特西╮ 提交于 2019-12-11 08:19:36

问题


i try to fetch data from web,but the page use a 302 redirect how can i use python to fetch the real url?


回答1:


Have a look at chapter 11.7. Handling redirects from the Dive Into Python series. It explains your entire issue in quite a bit of detail, example code and all.




回答2:


What are you currently using? Both urllib and urllib2 should handle it automatically:

page = urllib.urlopen('http://mrozekma.com/302test.php')
>>> print page.geturl()   # This will show the redirected-to URL
http://mrozekma.com/302test.php?success
>>> print page.readlines()
['Success']



回答3:


If you are using the http.client.HTTPConnection(3.x), or httplib.HTTPConnection(2.x), just obtain the Location Header:

response.getheader('Location')

I know this works at least on craigslist.org



来源:https://stackoverflow.com/questions/3887593/how-do-python-capture-302-redirect-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!