【发布时间】:2015-11-02 07:16:55
【问题描述】:
我正在尝试使用 Python 2.7 对 Wikipedia 文章的内容如何随时间变化进行一些分析。我只对页面的内容感兴趣,发现最简单的方法是使用Wikipedia package。我的代码成功加载了一篇文章,然后我可以使用 article.content 函数进行分析。
import Wikipdia
# pull in wikipedia article
name = 'George W. Bush'
article = wikipedia.page(name)
object = article.content
# Do analysis here
以下代码(我从另一个 Stack Exchange 问题中获得)查找我想要的所有修订历史记录。从中我可以提取我需要的所有修订 ID。
site = wiki.Wiki("http://en.wikipedia.org/w/api.php")
names = ["Sherrod Brown","Maria Cantwell"]
allMembers = []
for name in labels:
params = {'action':'query',
'titles': name,
'prop':'revisions',
'rvprop':'ids|flags|timestamp|userid|user|size|comment|tags|minor',
'rvlimit':'10'
}
req = api.APIRequest(site, params)
res = req.query(querycontinue=False)
allMembers.append(res)
# Write to a file and load it into a dictionary
with open('wiki-leg.json', 'w') as outfile:
json.dump(allMembers, outfile, indent=2)
with open('wiki-leg.json') as data_file:
data = json.load(data_file)
从这里我不确定如何获得相同的 article.content() 进行修订。我似乎无法使用 Wikipedia 包做到这一点,并且使用 urllib2 并没有为文本解析产生简单的结果。有没有办法获取维基百科文章的旧版本(修订)的内容?
【问题讨论】:
标签: python api urllib2 wikipedia-api