如何从维基百科提取纯文本

43 投票
13 回答
70457 浏览
提问于 2025-04-16 08:35

我想写一个脚本,只获取维基百科的描述部分。也就是说,当我输入

/wiki bla bla bla

时,它会去到这个关于 bla bla bla 的维基百科页面,获取以下内容,并把它返回到聊天室:

“Bla Bla Bla”是一首由Gigi D'Agostino创作的歌曲。他形容这首歌是“我写这首歌时想到的,所有那些说个不停却什么都没说的人”。这首歌中突出的但毫无意义的声音样本来自英国乐队Stretch的歌曲“Why Did You Do It”。

我该怎么做呢?

13 个回答

14

你可以以文本格式获取维基百科的数据。如果你需要访问很多标题的信息,可以一次性获取所有标题的维基数据。用竖线(|)来分隔每个标题。

http://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=Yahoo|Google&redirects=

这个API调用会返回Google和Yahoo的数据。

explaintext => 返回的内容是纯文本,而不是有限的HTML格式。

exlimit = max(现在是20);否则只会返回一个结果。

exintro => 只返回第一个部分之前的内容。如果你想要完整的数据,只需去掉这个选项。

redirects= 解决重定向的问题。

24

使用 MediaWiki API,这个接口是运行在维基百科上的。你需要自己处理一些数据解析的工作。

比如说:

http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&&titles=Bla%20Bla%20Bla

这段链接的意思是:

获取 (action=query) 主页 (title=Main%20Page) 最近一次的内容 (rvprop=content),并以 JSON 格式 (format=json) 返回。

你可能想要搜索一下查询结果,并使用第一个结果,这样可以处理拼写错误等问题。

51

这里有几种不同的方法,你可以选择适合你的那一种。下面的代码示例都使用了requests库来发送HTTP请求到API;如果你有Pip,可以通过命令pip install requests来安装这个库。所有示例都使用了Mediawiki API,其中两个示例使用了query接口;如果你想查看文档,可以点击这些链接。

1. 直接从API获取整个页面或页面“摘录”的纯文本表示

请注意,这种方法只适用于带有TextExtracts扩展的MediaWiki网站。这包括维基百科,但不包括一些较小的MediaWiki网站,比如http://www.wikia.com/

你需要访问一个类似于以下的URL:

https://en.wikipedia.org/w/api.php?action=query&format=json&titles=Bla_Bla_Bla&prop=extracts&exintro&explaintext

我们可以把这个链接拆解成以下几个参数(详细信息可以在https://www.mediawiki.org/wiki/Extension:TextExtracts#query+extracts找到):

  • action=queryformat=jsontitle=Bla_Bla_Bla都是MediaWiki API的标准参数
  • prop=extracts表示我们要使用TextExtracts扩展
  • exintro限制返回的内容为第一个章节标题之前的内容
  • explaintext使返回的摘录为纯文本,而不是HTML格式

然后解析JSON响应,提取出摘录内容:

>>> import requests
>>> response = requests.get(
...     'https://en.wikipedia.org/w/api.php',
...     params={
...         'action': 'query',
...         'format': 'json',
...         'titles': 'Bla Bla Bla',
...         'prop': 'extracts',
...         'exintro': True,
...         'explaintext': True,
...     }
... ).json()
>>> page = next(iter(response['query']['pages'].values()))
>>> print(page['extract'])
"Bla Bla Bla" is the title of a song written and recorded by Italian DJ Gigi D'Agostino. It was released in May 1999 as the third single from the album, L'Amour Toujours. It reached number 3 in Austria and number 15 in France. This song can also be heard in an added remixed mashup with L'Amour Toujours (I'll Fly With You) in its US radio version.

2. 使用parse接口获取页面的完整HTML,解析并提取第一段

MediaWiki有一个parse接口,你可以通过类似https://en.wikipedia.org/w/api.php?action=parse&page=Bla_Bla_Bla的URL来获取页面的HTML。然后你可以使用HTML解析器,比如lxml(先通过pip install lxml安装)来提取第一段内容。

例如:

>>> import requests
>>> from lxml import html
>>> response = requests.get(
...     'https://en.wikipedia.org/w/api.php',
...     params={
...         'action': 'parse',
...         'page': 'Bla Bla Bla',
...         'format': 'json',
...     }
... ).json()
>>> raw_html = response['parse']['text']['*']
>>> document = html.document_fromstring(raw_html)
>>> first_p = document.xpath('//p')[0]
>>> intro_text = first_p.text_content()
>>> print(intro_text)
"Bla Bla Bla" is the title of a song written and recorded by Italian DJ Gigi D'Agostino. It was released in May 1999 as the third single from the album, L'Amour Toujours. It reached number 3 in Austria and number 15 in France. This song can also be heard in an added remixed mashup with L'Amour Toujours (I'll Fly With You) in its US radio version.

3. 自己解析维基文本

你可以使用query API获取页面的维基文本,使用mwparserfromhell进行解析(先通过pip install mwparserfromhell安装),然后使用strip_code将其简化为人类可读的文本。需要注意的是,strip_code在写这段内容时并不是完美的(下面的示例清楚地显示了这一点),但希望未来会有所改进。

>>> import requests
>>> import mwparserfromhell
>>> response = requests.get(
...     'https://en.wikipedia.org/w/api.php',
...     params={
...         'action': 'query',
...         'format': 'json',
...         'titles': 'Bla Bla Bla',
...         'prop': 'revisions',
...         'rvprop': 'content',
...     }
... ).json()
>>> page = next(iter(response['query']['pages'].values()))
>>> wikicode = page['revisions'][0]['*']
>>> parsed_wikicode = mwparserfromhell.parse(wikicode)
>>> print(parsed_wikicode.strip_code())
{{dablink|For Ke$ha's song, see Blah Blah Blah (song). For other uses, see Blah (disambiguation)}}

"Bla Bla Bla" is the title of a song written and recorded by Italian DJ Gigi D'Agostino. It was released in May 1999 as the third single from the album, L'Amour Toujours. It reached number 3 in Austria and number 15 in France. This song can also be heard in an added remixed mashup with L'Amour Toujours (I'll Fly With You) in its US radio version.

Background and writing
He described this song as "a piece I wrote thinking of all the people who talk and talk without saying anything". The prominent but nonsensical vocal samples are taken from UK band Stretch's song "Why Did You Do It"''.

Music video
The song also featured a popular music video in the style of La Linea. The music video shows a man with a floating head and no arms walking toward what appears to be a shark that multiplies itself and can change direction. This style was also used in "The Riddle", another song by Gigi D'Agostino, originally from British singer Nik Kershaw.

Chart performance
Chart (1999-00)PeakpositionIreland (IRMA)Search for Irish peaks23

References

External links


Category:1999 singles
Category:Gigi D'Agostino songs
Category:1999 songs
Category:ZYX Music singles
Category:Songs written by Gigi D'Agostino

撰写回答