1 个回答
-
好的,似乎没有库支持它,因此我一直在使用以下代码:
import urllib2 import urlparse import re def get_hops(url): redirect_re = re.compile('<meta[^>]*?url=(.*?)["\']', re.IGNORECASE) hops = [] while url: if url in hops: url = None else: hops.insert(0, url) response = urllib2.urlopen(url) if response.geturl() != url: hops.insert(0, response.geturl()) # check for redirect meta tag match = redirect_re.search(response.read()) if match: url = urlparse.urljoin(url, match.groups()[0].strip()) else: url = None return hops