JBW*_*ore 8 python mediawiki wikipedia wikipedia-api mediawiki-api
我正在尝试编写一个python程序,可以在维基百科上搜索人们的出生和死亡日期.
例如,阿尔伯特爱因斯坦出生于1879年3月14日; 去世:1955年4月18日.
import urllib2
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
infile = opener.open('http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&rvsection=0&titles=Albert_Einstein&format=xml')
page2 = infile.read()
Run Code Online (Sandbox Code Playgroud)
这项工作尽可能地发挥作用.page2是来自Albert Einstein维基百科页面的部分的xml表示.
我看了这个教程,现在我有xml格式的页面... http://www.travisglines.com/web-coding/python-xml-parser-tutorial,但我不明白怎么弄我想要的信息(出生和死亡日期)来自xml.我觉得我必须亲近,但是,我不知道如何从这里开始.
编辑
经过几次回复后,我安装了BeautifulSoup.我现在正处于可以打印的阶段:
import BeautifulSoup as BS
soup = BS.BeautifulSoup(page2)
print soup.getText()
{{Infobox scientist
| name = Albert Einstein
| image = Einstein 1921 portrait2.jpg
| caption = Albert Einstein in 1921
| birth_date = {{Birth date|df=yes|1879|3|14}}
| birth_place = [[Ulm]], [[Kingdom of Württemberg]], [[German Empire]]
| death_date = {{Death date and age|df=yes|1955|4|18|1879|3|14}}
| death_place = [[Princeton, New Jersey|Princeton]], New Jersey, United States
| spouse = [[Mileva Mari?]]&nbsp;(1903–1919)<br>{{nowrap|[[Elsa Löwenthal]]&nbsp;(1919–1936)}}
| residence = Germany, Italy, Switzerland, Austria, Belgium, United Kingdom, United States
| citizenship = {{Plainlist|
* [[Kingdom of Württemberg|Württemberg/Germany]] (1879–1896)
* [[Statelessness|Stateless]] (1896–1901)
* [[Switzerland]] (1901–1955)
* [[Austria–Hungary|Austria]] (1911–1912)
* [[German Empire|Germany]] (1914–1933)
* United States (1940–1955)
}}
Run Code Online (Sandbox Code Playgroud)
所以,更接近,但我仍然不知道如何以这种格式返回death_date.除非我开始解析东西re?我可以这样做,但我觉得我会使用错误的工具来完成这项工作.
您可以考虑使用诸如BeautifulSoup或lxml之类的库来解析响应html/xml.
您可能还想查看一下Requests,它有一个更清晰的API来发出请求.
以下是使用的工作代码Requests,BeautifulSoup并且re可能不是最好的解决方案,但它非常灵活,可以针对类似的问题进行扩展:
import re
import requests
from bs4 import BeautifulSoup
url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&rvsection=0&titles=Albert_Einstein&format=xml'
res = requests.get(url)
soup = BeautifulSoup(res.text, "xml")
birth_re = re.search(r'(Birth date(.*?)}})', soup.revisions.getText())
birth_data = birth_re.group(0).split('|')
birth_year = birth_data[2]
birth_month = birth_data[3]
birth_day = birth_data[4]
death_re = re.search(r'(Death date(.*?)}})', soup.revisions.getText())
death_data = death_re.group(0).split('|')
death_year = death_data[2]
death_month = death_data[3]
death_day = death_data[4]
Run Code Online (Sandbox Code Playgroud)
Per @ JBernardo建议使用JSON数据,并mwparserfromhell为这个特定用例提供了更好的答案:
import requests
import mwparserfromhell
url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&rvsection=0&titles=Albert_Einstein&format=json'
res = requests.get(url)
text = res.json["query"]["pages"].values()[0]["revisions"][0]["*"]
wiki = mwparserfromhell.parse(text)
birth_data = wiki.filter_templates(matches="Birth date")[0]
birth_year = birth_data.get(1).value
birth_month = birth_data.get(2).value
birth_day = birth_data.get(3).value
death_data = wiki.filter_templates(matches="Death date")[0]
death_year = death_data.get(1).value
death_month = death_data.get(2).value
death_day = death_data.get(3).value
Run Code Online (Sandbox Code Playgroud)
首先,使用pywikipedia。它允许您通过高级抽象接口查询文章文本、模板参数等。其次,我会使用Persondata模板(请看文章末尾)。此外,从长远来看,您可能会对Wikidata感兴趣,这将需要几个月的时间才能引入,但它将使维基百科文章中的大多数元数据易于查询。