1

我正在尝试使用 pywikibot 从 wikidata 访问数据。我正在尝试使用数据对象的名称而不是代码来执行此操作。当我运行这个脚本时:

import pywikibot


site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()
token = repo.token(pywikibot.Page(repo, 'Main Page'), 'edit')

我收到此错误消息:

Traceback (most recent call last):
  File "/Users/this-user/PycharmProjects/teststuff/src/pywikibot_stuff/wikipedia/test.py", line 6, in <module>
    token = repo.token(pywikibot.Page(repo, 'Main Page'), 'edit')
  File "/Users/this-user/Library/Python/3.6/lib/python/site-packages/pywikibot/tools/__init__.py", line 1337, in wrapper
    return obj(*args, **kwargs)
  File "/Users/this-user/Library/Python/3.6/lib/python/site-packages/pywikibot/site.py", line 3495, in token
<class 'AssertionError'>
    return self.tokens[tokentype]
  File "/Users/this-user/Library/Python/3.6/lib/python/site-packages/pywikibot/site.py", line 1785, in __getitem__
    assert self.site.user(), 'User must login in this site'
AssertionError: User must login in this site
CRITICAL: Closing network session.

但是,这让我感到困惑,因为当我运行以下脚本时(Q9684 是纽约时报的 wikidata 代码):

import pywikibot

site = pywikibot.Site('wikidata', 'wikidata')
repo = site.data_repository()
item = pywikibot.ItemPage(repo, 'Q9684')

item_dict = item.get()
aliases = item_dict['aliases']
aliases = [aliases[key] for key in aliases]
aliases = [alias for sublist in aliases for alias in sublist]


print(aliases

一切正常,我得到:

['NY Times', 'New York Times', 'The Gray Lady', 'Gray Lady', 'NYT', 'TNYT', 'nytimes.com', 'New-York Daily Times', 'The New-York Times', 'NY Times', 'NY Times', 'New York Times', 'New York Times', 'NYT', 'NY Times', 'NY Times', 'New York Times', 'The Gray Lady', 'Gray Lady', 'The Grey Lady', 'Grey Lady', 'New York Times', 'NYT', '紐約時報', 'nytimes.com', 'New York Times', 'The New York Daily Times', 'NY Times', 'New York Times', 'NYT', 'The Gray Lady', 'The New York Times', 'Нью-Йорк Таймс', 'NY Times', 'New York Times', 'NYT', 'نيو يورك تايمز']

我也尝试过运行:

import pywikibot

site = pywikibot.Site('wikidata', 'wikidata')
repo = site.data_repository()
item = pywikibot.Page(site, 'New York Times')
item_dict = item.get()

print(item_dict)

但后来我得到了错误:

pywikibot.exceptions.NoPage: Page [[wikidata:New York Times]] doesn't exist.

我的 user-config.py 文件位于同一目录中,如下所示:

family = 'wikidata'
mylang = 'en'
usernames['wikidata']['wikidata'] = u'ExampleBot'

#console_encoding = 'utf-8'
#use_api_login = True

取消注释最后两行并没有什么不同。

有人知道这里的问题吗?为什么 pywikibot 在我搜索“纽约时报”时希望我登录,但在我使用代码时却不希望我登录?

4

2 回答 2

2

wikidata 中项目页面的标题是它们的'Q' id。所以

item = pywikibot.Page(site, 'New York Times')

创建一个不存在的页面:

>>> item.exists()
False

wikidata 站点的 item.get() 失败。你必须运行:

item = pywikibot.Page(site, 'Q9684')

获取令牌是为了编辑存储库中的内容,而不仅仅是检索并且您需要登录。

于 2017-06-20T05:31:24.247 回答
1

但后来我得到了错误:

pywikibot.exceptions.NoPage: Page [[wikidata:New York Times]] doesn't exist.

这是因为在 Wikidata 主命名空间中确实不存在名为“纽约时报”的页面。如果您知道确切的 Wikipedia 页面标题,并且想要获取他们的 Wikidata 项目 ID,则可以这样做:

wpsite = pywikibot.Site('en', 'wikipedia')
wppage = pywikibot.Page(wpsite, 'The New York Times')
item = pywikibot.ItemPage.fromPage(wppage) 

代替:

item = pywikibot.Page(site, 'New York Times') # this is wrong

实际上,如果您将使用框架函数,则不需要带有token = repo.token...的行来编辑 Wikidata。在此处查看更多详细信息并访问该页面底部列出的页面链接。

于 2017-07-27T17:05:24.077 回答