【问题标题】:Python - How to get the page Wikipedia will redirect me to?Python - 如何获取维基百科将我重定向到的页面?
【发布时间】:2017-11-28 17:33:08
【问题描述】:

我想存储几个不同的 Wikipedia 链接,但我不想将两个不同的链接存储到同一个页面两次。例如,以下链接不同,但它们指向同一个 Wikipedia 页面:

https://en.wikipedia.org/w/index.php?title=(1S)-1-Methyl-2,3,4,9-tetrahydro-1H-pyrido-3,4-b-indole&redirect=no 
https://en.wikipedia.org/w/index.php?title=(1S)-1-methyl-2,3,4,9-tetrahydro-1H-pyrido-3,4-b-indole&redirect=no
__________________________________________________|___________________________________________________________

唯一的区别是一个大写字符。或以下链接:

https://en.wikipedia.org/wiki/(0,1)-matrix 
https://en.wikipedia.org/wiki/(0,1)_matrix 
___________________________________|______ 

这只是不同的,因为一个有'-',另一个有'_'('')。所以我想要的是只存储其中一个或以下链接:

https://en.wikipedia.org/wiki/Tetrahydroharman 
https://en.wikipedia.org/wiki/Logical_matrix 

我已经尝试过这个SO question 的答案。但这对我不起作用。 (结果是我的初始 URL,而不是浏览器中将我重定向到的那个 wiki)那么我怎样才能实现我正在寻找的东西!?

【问题讨论】:

  • 您能否解释一下您通过尝试链接到的 SO 问题中的解决方案得到的结果以及您究竟尝试了哪些解决方案!?
  • @aran 我使用了接受的答案,我已经解释了我得到了什么。

标签: python python-3.x http python-requests url-redirection


【解决方案1】:

由于 Wikipedia 没有正确的 301/302 重定向,当您打开链接时会返回正确的 200 成功响应,然后使用 JS 更改 url

我想出了一个快速可行的解决方案。首先,从 URL 中删除 &redirect=no

In [42]: import requests

In [43]: r = requests.get('https://en.wikipedia.org/w/index.php?title=(1S)-1-Met
    ...: hyl-2,3,4,9-tetrahydro-1H-pyrido-3,4-b-indole')

In [44]: tmp = r.content.replace('<link rel="canonical" href="', 'r@ndom}-=||').
    ...: split('r@ndom}-=||')[-1]

In [45]: idx = tmp.find('"/>')

In [46]: real_link = tmp[:idx]

In [47]: real_link
Out[47]: 'https://en.wikipedia.org/wiki/Tetrahydroharman'

真正的URL值存储在&lt;link rel="canonical" href="标签中。

您可以使用上述方法,这对您的用例来说已经足够了,或者您可以使用 bs4 之类的库来解析页面并获取链接,或者使用正则表达式来提取链接。

【讨论】:

  • 没有get请求就没有别的办法了吗!?因为 get 请求相当重:-?
  • 是'r@ndom}-=||'只是您确定文本中不存在的东西!?
  • 顺便说一句,我认为您应该将 .content 更改为 .text
  • @thegod 因为 url 来自页面内容,没有更简单的方法。是的,r@ndom}-=||是我添加的一些随机字符串。它不应该在页面内容中。您可以使其更具可变性。
【解决方案2】:

您可以使用MediaWiki API 获取重定向的目标页面

结果可以是 JSON 格式(例如)

您只需要解析它以获取元素 to 或元素 title

的值

此查询将检索“Halab”的目标页面:

https://en.wikipedia.org/w/api.php?action=query&titles=Halab&&redirects&format=json

结果:

{  
   "batchcomplete":"",
   "query":{  
      "redirects":[  
         {  
            "from":"Halab",
            "to":"Aleppo"
         }
      ],
      "pages":{  
         "159244":{  
            "pageid":159244,
            "ns":0,
            "title":"Aleppo"
         }
      }
   }
}

在 Python 中:

import json
import requests

query = requests.get(r'https://en.wikipedia.org/w/api.php?action=query&titles={}&&redirects&format=json'.format('Halab'))

data = json.loads(query.text)

【讨论】:

    【解决方案3】:

    Amit Tripathi 的回答抛出异常。这是我的答案:

    res = requests.get(url)
    doc = lxml.html.fromstring(res.content)
    for t in doc.xpath("//link[contains(@rel, 'canonical')]"):
        new_url = str(t.attrib['href'])
    

    根据我的经验,可能会重定向到相同的网址。所以最好在使用 new_url 之前检查 (url != new_url)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      相关资源
      最近更新 更多