有没有办法获得维基百科网址的最终目的地?

2024-06-30 15:41:53 发布

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

我目前正在尝试获取一种鸟类的描述页面,使用它的学名来表示任何可用的语言。例如,使用学名“ibycteramericanus”作为“https://en.wikipedia.org/wiki/”的端点,浏览器将我重定向到the english wikipedia page。如果我使用子域“fr”,以法语获取它,我将被重定向to the french page。在

这一切在浏览器中运行得非常好,但我无法用python代码获得最终的url:

req = urllib2.Request("https://en.wikipedia.org/wiki/" + "Ibycter americanus")
res = urllib2.urlopen(req)
finalurl = res.geturl()
print(finalurl)

finalurl总是“https://en.wikipedia.org/wiki/Ibycter_americanus”,而不是浏览器中显示的“https://en.wikipedia.org/wiki/Red-throated_caracara”。有什么想法吗?在


Tags: thehttpsorgwikipage浏览器urllib2wikipedia