httplib 未获取所有重定向代码
我正在尝试获取似乎多次重定向的页面的最终网址。在浏览器中尝试此示例 URL,并将其与我的代码片段底部的最终 URL 进行比较:
这是我正在运行的测试代码,请注意,获得代码 200 的最终 URL 与浏览器中的 URL 不同。我有什么选择?
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import httplib
>>> from urlparse import urlparse
>>> url = 'http://www.usmc.mil/units/hqmc/'
>>> host = urlparse(url)[1]
>>> req = ''.join(urlparse(url)[2:5])
>>> conn = httplib.HTTPConnection(host)
>>> conn.request('HEAD', req)
>>> resp = conn.getresponse()
>>> print resp.status
301
>>> print resp.msg.dict['location']
http://www.marines.mil/units/hqmc/
>>> url = 'http://www.marines.mil/units/hqmc/'
>>> host = urlparse(url)[1]
>>> req = ''.join(urlparse(url)[2:5])
>>> conn = httplib.HTTPConnection(host)
>>> conn.request('HEAD', req)
>>> resp = conn.getresponse()
>>> print resp.status
302
>>> print resp.msg.dict['location']
http://www.marines.mil/units/hqmc/default.aspx
>>> url = 'http://www.marines.mil/units/hqmc/default.aspx'
>>> host = urlparse(url)[1]
>>> req = ''.join(urlparse(url)[2:5])
>>> conn = httplib.HTTPConnection(host)
>>> conn.request('HEAD', req)
>>> resp = conn.getresponse()
>>> print resp.status
200
>>> print resp.msg.dict['location']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'location'
>>> print url
http://www.marines.mil/units/hqmc/default.aspx //THIS URL DOES NOT RETURN A 200 IN ANY BROWSER I HAVE TRIED
I am trying to get the final url of a page that seems to redirect more than once. Try this sample URL in your browser and compare it to the final URL at the bottom of my code snippet:
Link that redirects more than once
And here is the test code I was running, notice the final URL that gets a code of 200 isn't the same as the one in your browser. What are my options?
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import httplib
>>> from urlparse import urlparse
>>> url = 'http://www.usmc.mil/units/hqmc/'
>>> host = urlparse(url)[1]
>>> req = ''.join(urlparse(url)[2:5])
>>> conn = httplib.HTTPConnection(host)
>>> conn.request('HEAD', req)
>>> resp = conn.getresponse()
>>> print resp.status
301
>>> print resp.msg.dict['location']
http://www.marines.mil/units/hqmc/
>>> url = 'http://www.marines.mil/units/hqmc/'
>>> host = urlparse(url)[1]
>>> req = ''.join(urlparse(url)[2:5])
>>> conn = httplib.HTTPConnection(host)
>>> conn.request('HEAD', req)
>>> resp = conn.getresponse()
>>> print resp.status
302
>>> print resp.msg.dict['location']
http://www.marines.mil/units/hqmc/default.aspx
>>> url = 'http://www.marines.mil/units/hqmc/default.aspx'
>>> host = urlparse(url)[1]
>>> req = ''.join(urlparse(url)[2:5])
>>> conn = httplib.HTTPConnection(host)
>>> conn.request('HEAD', req)
>>> resp = conn.getresponse()
>>> print resp.status
200
>>> print resp.msg.dict['location']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'location'
>>> print url
http://www.marines.mil/units/hqmc/default.aspx //THIS URL DOES NOT RETURN A 200 IN ANY BROWSER I HAVE TRIED
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 HttpLib2 获取 URL 的实际位置:
执行如下所示:
请注意这一点示例还使用了缓存(urllib 和 httplib 都不支持缓存)。因此,这将显着更快地重复运行。这对于爬行/抓取可能很有趣。如果您不需要缓存,请将
h = httplib2.Http(".cache_httplib")
替换为h = httplib2.Http()
。You can use HttpLib2 to get the actual location of an URL:
Execution looks like this:
Note this example also uses caching (which is not supported neither by urllib nor by httplib). So this will run repeatedly significantly faster. This might be interesting for crawling/scraping. If you do not want caching, replace
h = httplib2.Http(".cache_httplib")
withh = httplib2.Http()
.您可以尝试将 User-Agent 标头设置为浏览器的 User-Agent。
附:
urllib2 自动重定向
编辑:
you can try to set your User-Agent header to the User-Agent of your browser.
ps:
urllib2 automatically redirects
EDIT: