从python检测DNS重定向

2024-06-25 23:03:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python编写一个小的web抓取应用程序。该应用程序从一系列网站中获取数据,这些网站通常会重定向到一个新网站。有时,我所指向的网站并不存在。在本地计算机上发生这种情况时,ISP会将我重定向到另一个站点(使用<meta http-equiv="refresh">):

In [1]: import requests
In [2]: r = requests.get('http://www.asdflksdjk2.com')

In [3]: r.status_code
Out[3]: 200

In [4]: r.url
Out[4]: u'http://www.asdflksdjk2.com'

In [5]: r.text
Out[5]: u'<html><head><meta http-equiv="refresh" 
content="0;url=http://www.dnsrsearch.com/index.php?origURL=http://www.asdflksdjk2.com/"/>
</head><body><script>
window.location="http://www.dnsrsearch.com/index.php?origURL="+escape(window.location)+"&r="+escape(document.referrer);
</script></body></html>'

当我在digitalocean托管的vps上运行相同的代码时,我得到了不同的结果:

^{pr2}$

这次我被从页面重定向到这个gen.xyz站点:

In [5]: for hist in r.history:
            print hist.url, hist.status_code
Out[5]: 
http://www.asdflksdjk2.com 302
http://gen.xyz/availability-checker.php?src=www.asdflksdjk2 302

有没有一种可移植的方法来检测不再存在的页面?在


Tags: incom应用程序httpurl站点网站www