【问题标题】:How can I get an article from wiki with a specific language using python?如何使用 python 从 wiki 获取具有特定语言的文章?
【发布时间】:2020-06-15 23:17:39
【问题描述】:

我想在 wiki 中获取特定语言的文章。

我尝试了以下代码:

URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
        "action": "query",
        "titles": "Python",
        "prop": "langlinks",
        "lllang": "de",
        "format": "json"
        }
results = requests.get(url=URL, params=PARAMS)
soup = BeautifulSoup(results.content, 'html.parser')
print(soup.prettify())

但我没有得到整篇文章,我只知道这个

{"batchcomplete":"","query":{"pages":{"46332325":{"pageid":46332325,"ns":0,"title":"Python","langlinks":[{"lang":"de","*":"Python"}]}}}}

你能帮助理解我做错了什么吗?

【问题讨论】:

    标签: python-3.x beautifulsoup python-requests mediawiki


    【解决方案1】:

    将 URL 更改为 de.wikipedia.org 以获取德语版本。

    例如:

    import requests
    from bs4 import BeautifulSoup
    
    URL = "https://de.wikipedia.org/w/api.php"  # <-- note the de.
    PARAMS = {
            "action": "parse",
            "page": "Python (Programmiersprache)",
            "prop": "text",
            "section": 0,
            "format": "json"
            }
    
    results = requests.get(url=URL, params=PARAMS).json()
    soup = BeautifulSoup(results['parse']['text']['*'], 'html.parser')
    print(soup.prettify())
    

    打印:

    <div class="mw-parser-output">
     <table cellspacing="5" class="float-right infobox toccolours toptextcells" style="font-size:90%; margin-top:0; width:21em;">
      <tbody>
       <tr>
        <th class="hintergrundfarbe6" colspan="2" style="font-size:larger;">
         Python
        </th>
       </tr>
       <tr>
    
    ... and so on.
    

    要仅获取 wiki 模板/标签,您可以:

    URL = "https://de.wikipedia.org/w/api.php"
    PARAMS = {
            "action": "query",
            "titles": "Python (Programmiersprache)",
            "prop": "revisions",
            "rvprop": "content",
            "rvsection": 0,
            "format": "json"
            }
    
    results = requests.get(url=URL, params=PARAMS).json()
    print(results)
    

    【讨论】:

    • 但问题是我想要一个函数,它可以针对特定的标题和特定的语言为您提供该语言的文章。这就是为什么我需要通用 URL。
    • 为什么不根据您的功能编辑 API URL - 这仍然是“通用的”
    【解决方案2】:

    如果您有一种语言的维基百科页面标题,并且您想知道另一种语言的标题,您可以使用“langlinks”propertly,如下所示:

    https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&titles=Python+(programming+language)&lllang=de

    注意“lllang”设置为“de”

    这给了你:

    {
        "batchcomplete": "",
        "query": {
            "pages": {
                "23862": {
                    "pageid": 23862,
                    "ns": 0,
                    "title": "Python (programming language)",
                    "langlinks": [
                        {
                            "lang": "de",
                            "*": "Python (Programmiersprache)"
                        }
                    ]
                }
            }
        }
    }
    

    请参阅此处了解更多信息: https://www.mediawiki.org/wiki/API:Langlinks

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多