gpt4 book ai didi

python - 从维基百科解析出生和死亡日期?

转载 作者:太空狗 更新时间:2023-10-29 20:39:56 24 4
gpt4 key购买 nike

我正在尝试编写一个 python 程序,可以在维基百科中搜索人们的出生和死亡日期。

例如,爱因斯坦出生于:1879 年 3 月 14 日;卒于:1955 年 4 月 18 日。

我从 Fetch a Wikipedia article with Python 开始

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()

就目前而言,这是可行的。 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ć]] (1903–1919)<br>{{nowrap|[[Elsa Löwenthal]] (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)
}}

所以,更接近了,但我仍然不知道如何以这种格式返回 death_date。除非我开始用 re 解析东西?我可以做到,但我觉得我会为这项工作使用错误的工具。

最佳答案

您可以考虑使用类似 BeautifulSoup 的库或 lxml解析响应 html/xml。

您可能还想看看 Requests ,它有一个更简洁的 API 来发出请求。


这是使用 RequestsBeautifulSoupre 的工作代码,可以说不是这里最好的解决方案,但它非常灵活并且可以针对类似问题进行扩展:

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]

根据@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

关于python - 从维基百科解析出生和死亡日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12250580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com