如何从Wikidata Id获取维基百科页面



如何从Wikidata Id(例如:Q19675)中获取维基百科页面(用特定语言,比如法语)?这个问题看起来很明显,但奇怪的是,我在网上什么也没找到。我正在寻找一个可以用于请求Python模块的url命令,比如:

url = "https://www.wikidata.org/w/api.php?action=some_method&ids=Q19675"
r = requests.post(url, headers={"User-Agent" : "Magic Browser"})

有人能帮我吗?

您必须使用带有action=wbgetentities:的MediaWiki API

https://www.wikidata.org/w/api.php?action=wbgetentities&format=xml&props=sitelinks&ids=Q19675&sitefilter=frwiki

其中:

  • ids=Q19675-维基数据项ID
  • sitefilter=frwiki-只获取法语维基百科的页面标题

例如,您的回复将是:

<api success="1">
    <entities>
        <entity type="item" id="Q19675">
            <sitelinks>
                <sitelink site="frwiki" title="Musée du Louvre">
                    <badges/>
                </sitelink>
            </sitelinks>
        </entity>
    </entities>
</api>

在python中,您可以执行以下操作:

def get_wikipedia_url_from_wikidata_id(wikidata_id, lang='en', debug=False):
    import requests
    from requests import utils
    url = (
        'https://www.wikidata.org/w/api.php'
        '?action=wbgetentities'
        '&props=sitelinks/urls'
        f'&ids={wikidata_id}'
        '&format=json')
    json_response = requests.get(url).json()
    if debug: print(wikidata_id, url, json_response) 
    entities = json_response.get('entities')    
    if entities:
        entity = entities.get(wikidata_id)
        if entity:
            sitelinks = entity.get('sitelinks')
            if sitelinks:
                if lang:
                    # filter only the specified language
                    sitelink = sitelinks.get(f'{lang}wiki')
                    if sitelink:
                        wiki_url = sitelink.get('url')
                        if wiki_url:
                            return requests.utils.unquote(wiki_url)
                else:
                    # return all of the urls
                    wiki_urls = {}
                    for key, sitelink in sitelinks.items():
                        wiki_url = sitelink.get('url')
                        if wiki_url:
                            wiki_urls[key] = requests.utils.unquote(wiki_url)
                    return wiki_urls
    return None   

如果运行get_wikipedia_url_from_wikidata_id("Q182609", lang='en'),则返回:'https://en.wikipedia.org/wiki/Jacques_Rogge'

如果运行get_wikipedia_url_from_wikidata_id("Q182609", lang=None),则返回:

{'afwiki': 'https://af.wikipedia.org/wiki/Jacques_Rogge',
 'alswiki': 'https://als.wikipedia.org/wiki/Jacques_Rogge',
 'arwiki': 'https://ar.wikipedia.org/wiki/جاك_روج',
 'azbwiki': 'https://azb.wikipedia.org/wiki/ژاک_روق',
 'azwiki': 'https://az.wikipedia.org/wiki/Jak_Roqqe',
 'bclwiki': 'https://bcl.wikipedia.org/wiki/Jacques_Rogge',
 'bgwiki': 'https://bg.wikipedia.org/wiki/Жак_Рох',
 'bnwiki': 'https://bn.wikipedia.org/wiki/জ্যাকুয়েস_রগ',
 'cawiki': 'https://ca.wikipedia.org/wiki/Jacques_Rogge',
 'commonswiki': 'https://commons.wikimedia.org/wiki/Category:Jacques_Rogge',
 'cswiki': 'https://cs.wikipedia.org/wiki/Jacques_Rogge',
 'cswikiquote': 'https://cs.wikiquote.org/wiki/Jacques_Rogge',
 'cywiki': 'https://cy.wikipedia.org/wiki/Jacques_Rogge',
 'dawiki': 'https://da.wikipedia.org/wiki/Jacques_Rogge',
 'dewiki': 'https://de.wikipedia.org/wiki/Jacques_Rogge',
 'elwiki': 'https://el.wikipedia.org/wiki/Ζακ_Ρογκ',
 'enwiki': 'https://en.wikipedia.org/wiki/Jacques_Rogge',
 'eowiki': 'https://eo.wikipedia.org/wiki/Jacques_Rogge',
 'eswiki': 'https://es.wikipedia.org/wiki/Jacques_Rogge',
 'etwiki': 'https://et.wikipedia.org/wiki/Jacques_Rogge',
 'fawiki': 'https://fa.wikipedia.org/wiki/ژاک_روگ',
 'fiwiki': 'https://fi.wikipedia.org/wiki/Jacques_Rogge',
 'frwiki': 'https://fr.wikipedia.org/wiki/Jacques_Rogge',
 'hewiki': "https://he.wikipedia.org/wiki/ז'אק_רוג",
 'hrwiki': 'https://hr.wikipedia.org/wiki/Jacques_Rogge',
 'huwiki': 'https://hu.wikipedia.org/wiki/Jacques_Rogge',
 'idwiki': 'https://id.wikipedia.org/wiki/Jacques_Rogge',
 'itwiki': 'https://it.wikipedia.org/wiki/Jacques_Rogge',
 'itwikiquote': 'https://it.wikiquote.org/wiki/Jacques_Rogge',
 'jawiki': 'https://ja.wikipedia.org/wiki/ジャック・ロゲ',
 'kkwiki': 'https://kk.wikipedia.org/wiki/Жак_Рогге',
 'kowiki': 'https://ko.wikipedia.org/wiki/자크_로게',
 'ltwiki': 'https://lt.wikipedia.org/wiki/Jacques_Rogge',
 'lvwiki': 'https://lv.wikipedia.org/wiki/Žaks_Roge',
 'mkwiki': 'https://mk.wikipedia.org/wiki/Жак_Рог',
 'mnwiki': 'https://mn.wikipedia.org/wiki/Жак_Рогге',
 'mswiki': 'https://ms.wikipedia.org/wiki/Jacques_Rogge',
 'nlwiki': 'https://nl.wikipedia.org/wiki/Jacques_Rogge',
 'nowiki': 'https://no.wikipedia.org/wiki/Jacques_Rogge',
 'plwiki': 'https://pl.wikipedia.org/wiki/Jacques_Rogge',
 'ptwiki': 'https://pt.wikipedia.org/wiki/Jacques_Rogge',
 'rowiki': 'https://ro.wikipedia.org/wiki/Jacques_Rogge',
 'ruwiki': 'https://ru.wikipedia.org/wiki/Рогге,_Жак',
 'ruwikinews': 'https://ru.wikinews.org/wiki/Категория:Жак_Рогге',
 'scowiki': 'https://sco.wikipedia.org/wiki/Jacques_Rogge',
 'simplewiki': 'https://simple.wikipedia.org/wiki/Jacques_Rogge',
 'skwiki': 'https://sk.wikipedia.org/wiki/Jacques_Rogge',
 'srwiki': 'https://sr.wikipedia.org/wiki/Жак_Рог',
 'svwiki': 'https://sv.wikipedia.org/wiki/Jacques_Rogge',
 'thwiki': 'https://th.wikipedia.org/wiki/ฌัก_โรคเคอ',
 'tlwiki': 'https://tl.wikipedia.org/wiki/Jacques_Rogge',
 'trwiki': 'https://tr.wikipedia.org/wiki/Jacques_Rogge',
 'ukwiki': 'https://uk.wikipedia.org/wiki/Жак_Рогге',
 'viwiki': 'https://vi.wikipedia.org/wiki/Jacques_Rogge',
 'wuuwiki': 'https://wuu.wikipedia.org/wiki/雅克·罗格',
 'zhwiki': 'https://zh.wikipedia.org/wiki/雅克·罗格'}

一旦你得到维基百科的网址,你也应该通过重定向来解决它们。

def get_redirect_urls(wikipedia_url_ids, retry=0, max_retry=10, backoff=2, debug=False):
    import json
    import requests   
    import time
    wikipedia_url_ids_encoded = [requests.utils.quote(x) for x in wikipedia_url_ids]
    request_url=r'https://en.wikipedia.org/w/api.php?action=query&titles={}&&redirects&format=json'.format("|".join(wikipedia_url_ids_encoded))
    if debug: print(request_url)
    response = requests.get(request_url)
    if retry == max_retry:
        return None
    if response.status_code == 429:
        # Too many requests
        wait = backoff * (2 ** (retry - 1))
        time.sleep(wait)
        return get_redirect_urls(wikipedia_url_ids, retry=retry + 1)
    response_json = json.loads(response.text)
    if debug: print(response_json)
    if 'query' not in response_json:
        raise RuntimeError(f'impossible to parse request: {request_url}, response: {reponse_json}')
    normalized = {x['from']: x['to'] for x in response_json.get('query').get('normalized', [])}
    redirects = {x['from']: x['to'] for x in response_json.get('query').get('redirects', [])}
    normalized_url_id_dict = {x: normalized.get(x, x) for x in wikipedia_url_ids}
    return {x: redirects[normalized_url_id_dict[x]].replace(' ', '_') for x in wikipedia_url_ids 
            if normalized_url_id_dict[x] in redirects}

如果删除主机名前缀,只从URL路径中提取页面的标识符,则可以获得重定向URL的字典。

"Military_Academy_Commander_in_Chief_Hugo_Rafael_Chávez_Frías","Valley_Children’s_Hospital", 
                  "Park Jung-suk (video game player)", "Valley Children's Hospital",
                   "Troop Officers Military College",
                  "Hobbs_&_Shaw", "Schematic_Records", "Ethan_Klein", "Hobbs & Shaw"], debug=True)

将打印并返回:

https://en.wikipedia.org/w/api.php?action=query&titles=Tom_Cruise|Tom%20Cruise|Mark_Daniel_Gangloff|Park_Jung-suk_%28gamer%29|Military_Academy_Commander_in_Chief_Hugo_Rafael_Ch%C3%A1vez_Fr%C3%ADas|Valley_Children%E2%80%99s_Hospital|Park%20Jung-suk%20%28video%20game%20player%29|Valley%20Children%27s%20Hospital|Troop%20Officers%20Military%20College|Hobbs_%26_Shaw|Schematic_Records|Ethan_Klein|Hobbs%20%26%20Shaw&&redirects&format=json
{'batchcomplete': '', 'query': {'normalized': [{'from': 'Tom_Cruise', 'to': 'Tom Cruise'}, {'from': 'Mark_Daniel_Gangloff', 'to': 'Mark Daniel Gangloff'}, {'from': 'Park_Jung-suk_(gamer)', 'to': 'Park Jung-suk (gamer)'}, {'from': 'Military_Academy_Commander_in_Chief_Hugo_Rafael_Chávez_Frías', 'to': 'Military Academy Commander in Chief Hugo Rafael Chávez Frías'}, {'from': 'Valley_Children’s_Hospital', 'to': 'Valley Children’s Hospital'}, {'from': 'Hobbs_&_Shaw', 'to': 'Hobbs & Shaw'}, {'from': 'Schematic_Records', 'to': 'Schematic Records'}, {'from': 'Ethan_Klein', 'to': 'Ethan Klein'}], 'redirects': [{'from': 'Schematic Records', 'to': 'Intelligent dance music'}, {'from': 'Ethan Klein', 'to': 'H3h3Productions'}, {'from': "Valley Children's Hospital", 'to': 'Valley Children’s Hospital'}, {'from': 'Park Jung-suk (video game player)', 'to': 'Park Jung-suk (gamer)'}, {'from': 'Troop Officers Military College', 'to': 'Military Academy Commander in Chief Hugo Rafael Chávez Frías'}, {'from': 'Mark Daniel Gangloff', 'to': 'Mark Gangloff'}], 'pages': {'55632122': {'pageid': 55632122, 'ns': 0, 'title': 'Hobbs & Shaw'}, '43243423': {'pageid': 43243423, 'ns': 0, 'title': 'Military Academy Commander in Chief Hugo Rafael Chávez Frías'}, '3742745': {'pageid': 3742745, 'ns': 0, 'title': 'Park Jung-suk (gamer)'}, '31460': {'pageid': 31460, 'ns': 0, 'title': 'Tom Cruise'}, '63813213': {'pageid': 63813213, 'ns': 0, 'title': 'Valley Children’s Hospital'}, '49550675': {'pageid': 49550675, 'ns': 0, 'title': 'H3h3Productions'}, '81213': {'pageid': 81213, 'ns': 0, 'title': 'Intelligent dance music'}, '9316111': {'pageid': 9316111, 'ns': 0, 'title': 'Mark Gangloff'}}}}
{'Mark_Daniel_Gangloff': 'Mark_Gangloff',
 'Park Jung-suk (video game player)': 'Park_Jung-suk_(gamer)',
 "Valley Children's Hospital": 'Valley_Children’s_Hospital',
 'Troop Officers Military College': 'Military_Academy_Commander_in_Chief_Hugo_Rafael_Chávez_Frías',
 'Schematic_Records': 'Intelligent_dance_music',
 'Ethan_Klein': 'H3h3Productions'}

首先,使用Python和Pip,安装Wikipedia库,还安装Wikidata图书馆:

https://pypi.org/project/Wikidata/

现在,假设您获得了您的WikidataId(例如:Q1617977),这样您就可以从WikidataId中读取实体.data

只需打印实体.data,您就会看到它类似于json。

因此,您可以迭代到以从您的WikidataId中获取其他信息,例如:

['sitelinks']['rwiki']['url'][strong>

from wikidata.client import Client
import wikipedia
client = Client()
entity = client.get('Q1617977', load=True)
url = entity.data['sitelinks']['frwiki']['url']
print(url)

查询还可以返回数据JSON格式,即对于Q228865:

https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&props=sitelinks&ids=Q228865&sitefilter=enwiki

退货:

{实体:{Q228865}:{类型:项目,id:站点链接Wasikowska"徽章":[]}}}},";"成功":1}

"标题";然后可用于制作url;空间还可以

https://en.wikipedia.org/wiki/MiaWasikowska

这也有效(自动重定向,或用下划线替换空格)

https://en.wikipedia.org/wiki/Mia_Wasikowska

相关内容

  • 没有找到相关文章

最新更新