在urllib2.urlopen()
的结果上调用geturl()
方法。 geturl()
"返回检索到的资源的URL,通常用于确定是否遵循了重定向。"
例如:
#!/usr/bin/env python
# coding: utf-8
import urllib2
url1 = 'http://www.independent.co.uk/life-style/gadgets-and-tech/news/chinese-blamed-for-gmail-hacking-2292113.html'
url2 = 'http://www.independent.co.uk/life-style/gadgets-and-tech/news/2292113.html'
for url in [url1, url2]:
result = urllib2.urlopen(url)
print result.geturl()
输出结果为:
http://www.independent.co.uk/life-style/gadgets-and-tech/news/chinese-blamed-for-gmail-hacking-2292113.html
http://www.independent.co.uk/life-style/gadgets-and-tech/news/chinese-blamed-for-gmail-hacking-2292113.html
显然,仅从URL中无法确定这一点。
您可以获取内容并进行比较,但我想您需要使用智能标准来决定何时两个页面相同——例如,两个页面都指向同一篇文章,但是随机广告不同,或者相关文章因其他因素而改变。
设计您的程序以便匹配页面的标准易于替换,甚至是动态替换,并尝试直到找到一个不失败的标准——例如,对于报纸页面,您可以尝试查找标题。