首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >URL重定向问题

URL重定向问题
EN

Stack Overflow用户
提问于 2010-02-14 16:40:27
回答 2查看 448关注 0票数 1

我有下面的网址

http://bit.ly/cDdh1c

当您在浏览器中放置上述url并按enter键时,它将重定向到下面的url http://www.kennystopproducts.info/Top/?hop=arnishad

但是,当我试图通过python程序(下面可以看到代码)为上面的url http://bit.ly/cDdh1c找到相同的基url (在消除了所有重定向url之后)时,我获得了下面的url http://www.cbtrends.com/作为基url.Please,参见下面的日志文件

为什么相同的url在浏览器和python program.What中的行为不同,我应该在python程序中进行更改,以便它可以重定向到正确的url?我想知道这种奇怪的行为是如何发生的。

观察到类似行为的其他url是

  1. http://bit.ly/bEKyOx ====> http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&item=150413977509 (通过browser)
  2. http://www.ebay.com (通过python程序))

如果urlparse.urlsplit(host.strip()) == 0:返回None:===httplib.HTTPConnection( timeout=10) connection.request("HEAD“),(路径) resp = connection.getresponse(),除了:不返回最大的尝试=最大的尝试-1 if (resp.status >= 300)和(resp.status <= 399):self.logger.debug(“当前的%s是重定向的%s”)% turl ) turl = resp.getheader('location') elif (resp.status >= 200)和(resp.status <= 299):self.logger.debug(“当前url %s是正确的url %s是正确的”%turl)返回turl:#一些问题,此url返回无返回。

用于参考的日志文件

代码语言:javascript
复制
2010-02-14 10:29:43,260 - paypallistener.views.MrCrawler - INFO - Bringing down the base URL
2010-02-14 10:29:43,261 - paypallistener.views.MrCrawler - DEBUG - what is the url=http://bit.ly/cDdh1c
2010-02-14 10:29:43,994 - paypallistener.views.MrCrawler - DEBUG - The present http://bit.ly/cDdh1c is a redirection one
2010-02-14 10:29:43,995 - paypallistener.views.MrCrawler - DEBUG - what is the url=http://www.cbtrends.com/get-product.html?productid=reFfJcmpgGt95hoiavbXUAYIMP7OfiQn0qBA8BC7%252BV8%253D&affid=arnishad&tid=arnishad&utm_source=twitterfeed&utm_medium=twitter
2010-02-14 10:29:44,606 - paypallistener.views.MrCrawler - DEBUG - The present http://www.cbtrends.com/get-product.html?productid=reFfJcmpgGt95hoiavbXUAYIMP7OfiQn0qBA8BC7%252BV8%253D&affid=arnishad&tid=arnishad&utm_source=twitterfeed&utm_medium=twitter is a redirection one
2010-02-14 10:29:44,607 - paypallistener.views.MrCrawler - DEBUG - what is the url=http://www.cbtrends.com/
2010-02-14 10:29:45,547 - paypallistener.views.MrCrawler - DEBUG - The present url http://www.cbtrends.com/ is a proper one
http://www.cbtrends.com/
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-14 17:22:53

您的问题是,当您调用urlsplit时,path变量只包含路径,并且缺少查询。

所以,你可以试试:

代码语言:javascript
复制
import httplib
import urlparse

def getUrl(url):
    maxattempts = 10
    turl = url
    while (maxattempts  >  0) :               
        host,path,query = urlparse.urlsplit(turl)[1:4]
        if  len(host.strip()) == 0 :
            return None
        try: 
            connection = httplib.HTTPConnection(host,timeout=10)
            connection.request("GET", path+'?'+query)
            resp = connection.getresponse()
        except:                         
            return None                     
        maxattempts = maxattempts - 1
        if (resp.status >= 300) and (resp.status <= 399):
            turl = resp.getheader('location')
        elif (resp.status >= 200) and (resp.status <= 299) :
            return turl
        else :
            #some problem with this url
            return None               
    return None
print getUrl('http://bit.ly/cDdh1c')
票数 1
EN

Stack Overflow用户

发布于 2010-02-14 17:53:38

你的问题来自于这句话:

代码语言:javascript
复制
host,path = urlparse.urlsplit(turl)[1:3]

您忽略了查询字符串。因此,在您提供的示例日志中,您要执行的第二个HEAD请求将在没有GET参数的http://www.cbtrends.com/get-product.html上进行。打开浏览器中的URL,您将看到它重定向到http://www.cbtrends.com/

您必须使用计算路径-- urlsplit返回的元组的所有元素。

代码语言:javascript
复制
parts = urlparse.urlsplit(turl)
host = parts[1]
path = "%s?%s#%s" % parts[2:5]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2261823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档