我正在尝试使用pywikibot访问维基数据中的数据。我试图用数据对象的名称而不是代码来完成这个任务。当我运行这个脚本时:
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是“纽约时报”的维基数据代码):
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想让我在搜索“纽约时报”时登录,而不是在我使用代码时登录?
发布于 2017-06-20 05:31:24
维基数据中项目页面的标题是它们的“Q”id。因此
item = pywikibot.Page(site, 'New York Times')创建不存在的页:
>>> item.exists()
False而item.get()在维基数据网站上失败了。你得跑:
item = pywikibot.Page(site, 'Q9684')使用令牌是为了编辑存储库中的内容,而不仅仅是检索,为此您需要登录。
发布于 2017-07-27 17:05:24
但是我得到了一个错误:
pywikibot.exceptions.NoPage: Page [[wikidata:New York Times]] doesn't exist.这是因为在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。请参阅详细信息这里和访问该页面底部列出的页面链接。
https://stackoverflow.com/questions/44623245
复制相似问题