2016-04-04 3 views
1

特定のラベルと説明を持つアイテムがWikidataに既に存在するかどうかを調べる方法を探しています。この作業は、Pywikibotによって実行される必要があります。ボットがすでに存在する場合、ボットに新しいアイテムを作成させたくありません。これまでのところ、私のコードは次のようになります。特定のラベルと説明を持つアイテムがPywikibotによってWikidataに既に存在するかどうか確認してください

...     
def check_item_existence(self): 
    transcript_file = self.transcript_file 
    with open(transcript_file) as csvfile: 
     transcript_dict = csv.DictReader(csvfile, delimiter="\t") 
     for row in transcript_dict: 
      site = pywikibot.Site("en", "TillsWiki") 
      existing_item = pywikibot.ItemPage(site, row['Name']) 
      title = existing_item.title() 

答えて

0

あなたはWikibase APIからwbsearchentityを使用することができます。コードは、特定の英語のラベルを持つすべての項目がウィキデータに存在するかどうかを確認することです:ウィキデータのラベルは一意ではなく、エイリアスのためにそのAPIの検索だけでなく

from pywikibot.data import api 
... 
def wikiitemexists(label): 
    params = {'action': 'wbsearchentities', 'format': 'json', 
       'language': 'en', 'type': 'item', 'limit':1, 
       'search': label} 
    request = api.Request(site=acta_site, **params) 
    result = request.submit() 
    return True if len(result['search'])>0 else False 

ていることに注意してください。

関連する問題