Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python httplib没有获取所有重定向代码_Python_Httplib - Fatal编程技术网

Python httplib没有获取所有重定向代码

Python httplib没有获取所有重定向代码,python,httplib,Python,Httplib,我正在尝试获取一个页面的最终url,该页面似乎多次重定向。在浏览器中尝试此示例URL,并将其与“我的代码段”底部的最终URL进行比较: 这是我正在运行的测试代码,请注意,代码为200的最终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 inform

我正在尝试获取一个页面的最终url,该页面似乎多次重定向。在浏览器中尝试此示例URL,并将其与“我的代码段”底部的最终URL进行比较:

这是我正在运行的测试代码,请注意,代码为200的最终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 
Python2.7.1+(r271:868321911年4月11日18:13:53)
[GCC 4.5.2]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入httplib
>>>从URLPRASE导入URLPRASE
>>>url='1〕http://www.usmc.mil/units/hqmc/'
>>>主机=url解析(url)[1]
>>>请求=''.join(url解析(url)[2:5])
>>>conn=httplib.HTTPConnection(主机)
>>>连接请求(“头”,请求)
>>>resp=conn.getresponse()
>>>打印响应状态
301
>>>打印响应消息dict['location']
http://www.marines.mil/units/hqmc/
>>>url='1〕http://www.marines.mil/units/hqmc/'
>>>主机=url解析(url)[1]
>>>请求=''.join(url解析(url)[2:5])
>>>conn=httplib.HTTPConnection(主机)
>>>连接请求(“头”,请求)
>>>resp=conn.getresponse()
>>>打印响应状态
302
>>>打印响应消息dict['location']
http://www.marines.mil/units/hqmc/default.aspx
>>>url='1〕http://www.marines.mil/units/hqmc/default.aspx'
>>>主机=url解析(url)[1]
>>>请求=''.join(url解析(url)[2:5])
>>>conn=httplib.HTTPConnection(主机)
>>>连接请求(“头”,请求)
>>>resp=conn.getresponse()
>>>打印响应状态
200
>>>打印响应消息dict['location']
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
KeyError:“位置”
>>>打印url
http://www.marines.mil/units/hqmc/default.aspx //在我尝试过的任何浏览器中,此URL都不会返回200

您可以尝试将用户代理标题设置为浏览器的用户代理

附言: urllib2自动重定向

编辑:

您可以使用获取URL的实际位置:

import httplib2

def getContentLocation(link):
    h = httplib2.Http(".cache_httplib")
    h.follow_all_redirects = True
    resp = h.request(link, "GET")[0]
    contentLocation = resp['content-location']
    return contentLocation

if __name__ == '__main__':
    link = 'http://podcast.at/podcast_url344476.html'
    print getContentLocation(link)
执行过程如下所示:

$ python2.7 getContentLocation.py
http://keyinvest.podcaster.de/8uhr30.rss

注意:本例还使用缓存(urllib和httplib都不支持缓存)。因此,这将以显著更快的速度重复运行。这对于爬行/抓取可能很有趣。如果不需要缓存,请将
h=httplib2.Http(“.cache\u httplib”)
替换为
h=httplib2.Http()

,我知道,但urllib2没有告诉我最终正确的url是什么。这就是我想要找到的。打开你得到的最后一个URL
http://www.marines.mil/units/hqmc/default.aspx
在浏览器中。它重定向到
http://www.marines.mil/unit/hqmc/Pages/default.aspx
。我检查了chrome和firefox中的服务器响应代码。当python说可能存在javascript或html?jep重定向时,这些浏览器检测到302重定向,
Nice catch。我也看到了。我会找到一个关于捕获元刷新重定向,甚至javascript重定向的好链接。谢谢
$ python2.7 getContentLocation.py
http://keyinvest.podcaster.de/8uhr30.rss