抓取网址文件以在重定向后获取最终目标网址?

时间:2014-06-26 20:49:59

标签: python

我有一个文本文件如下

http://url.com/r/GFDGDFG
http://url.com/r/A32512F
http://url.com/r/AWDRE25
http://url.com/r/AGBFD35
http://url.com/r/NFG4622

有没有办法使用python(试图在我的linux盒子上获取一些简单的脚本)抓取每个URL(加载它)并获取最终的URL?每个网址/链接都是重定向,我想在重定向后获取网址。

1 个答案:

答案 0 :(得分:1)

import requests

urls = '''http://url.com/r/GFDGDFG
http://url.com/r/A32512F
http://url.com/r/AWDRE25
http://url.com/r/AGBFD35
http://url.com/r/NFG4622'''

for x in urls.splitlines():
    r = requests.get(x)
    print x, '=>', r.url

结果:

http://url.com/r/GFDGDFG => http://lyrics.url.com
http://url.com/r/A32512F => http://lyrics.url.com
http://url.com/r/AWDRE25 => http://lyrics.url.com
http://url.com/r/AGBFD35 => http://lyrics.url.com
http://url.com/r/NFG4622 => http://lyrics.url.com