我在Python中有以下代码,它仅提取有关“人工智能”的文章的介绍,而我想提取所有子部分(历史,目标......)
import requests
def get_wikipedia_page(page_title):
endpoint = "https://en.wikipedia.org/w/api.php"
params = {
"format": "json",
"action": "query",
"prop": "extracts",
"exintro": "",
"explaintext": "",
"titles": page_title
}
response = requests.get(endpoint, params=params)
data = response.json()
pages = data["query"]["pages"]
page_id = list(pages.keys())[0]
return pages[page_id]["extract"]
page_title = "Artificial intelligence"
wikipedia_page = get_wikipedia_page(page_title)
有人建议使用另一种方法,解析html并使用BeautifulSoup转换为文本:
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = "https://en.wikipedia.org/wiki/Artificial_intelligence"
html = urlopen(url).read()
soup = BeautifulSoup(html, features="html.parser")
# kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
# get text
text = soup.get_text()
# break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in
line.split("
"))
# drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)
print(text)
这不是一个足够好的解决方案,因为它包括网站上出现的所有文本(如图像文本),并且包括文本中的引用(例如 [1]),而第一个脚本删除了它们。
我怀疑维基百科的api应该提供一个更优雅的解决方案,如果只能得到第一部分就太奇怪了?
就像在我们的网络浏览器中一样,我们可以通过 URL 检索完整的维基百科页面,并使用 Beautiful Soup 解析 HTML 响应。
作为替代方案,我们可以使用 API,请参阅维基百科的 API 文档。
当将
action=query
与 format=json
一起使用时,您可以使用这 4 个选项进行文本提取:
titles=Artificial intelligence
页面prop=extracts
使用 TextExtracts 扩展exintro
限制对第一个部分标题之前的内容的响应(删除此内容以获取包括部分的整个文本)explaintext
提取为纯文本响应而不是 HTML要检索部分,请使用
action=parse
和 format=json
以及这些选项:
page=Artificial intelligence
获取此页面的内容prop=sections
仅返回部分还有一个 API 沙箱,您可以在其中尝试多个参数。 生成的 GET 请求将检索示例页面“人工智能”的所有部分: https://en.wikipedia.org/wiki/Special:ApiSandbox#action=parse&format=json&page=Artificial%20intelligence&prop=sections&formatversion=2
这将使用包含所有部分的 JSON 进行响应:
{
"parse": {
"title": "Artificial intelligence",
"pageid": 1164,
"sections": [
{
"toclevel": 1,
"level": "2",
"line": "History",
"number": "1",
"index": "1",
"fromtitle": "Artificial_intelligence",
"byteoffset": 5987,
"anchor": "History",
"linkAnchor": "History"
}
}
(简化,只保留第一部分)
要获取这些部分之一的文本,请将该部分指定为查询参数(通过 id 或标题),例如
section=1§iontitle=History
:
https://en.wikipedia.org/wiki/Special:ApiSandbox#action=parse&format=json&page=Artificial_intelligence§ion=1§iontitle=History&formatversion=2
这将检索文本(HTML 格式):
{
"parse": {
"title": "Artificial intelligence",
"pageid": 1164,
"revid": 1126677096,
"text": "<div class=\"mw-parser-output\"><h2><span class=\"mw-headline\" id=\"History\">History</span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\">[</span><a href=\"/w/index.php?title=Artificial_intelligence&action=edit&section=1\" title=\"Edit section: History\">edit</a><span class=\"mw-editsection-bracket\">]</span></span></h2>\n<style data-mw-deduplicate=\"TemplateStyles:r1033289096\">.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}</style><div role=\"note\" class=\"hatnote navigation-not-searchable\">Main articles: <a href=\"/wiki/History_of_artificial_intelligence\" title=\"History of artificial intelligence\">History of artificial intelligence</a> and <a href=\"/wiki/Timeline_of_artificial_intelligence\" title=\"Timeline of artificial intelligence\">Timeline of artificial intelligence</a>
注意:以上回复已被截断,仅显示文本示例。
虽然上面的文本内容被格式化为 HTML,但可能有选项将其设置为纯文本。
你也可以像使用Python
wikipedia
:import wikpedia
wikipedia.set_lang('en')
page = wikipedia.page('Artificial intelligence')
print(page.content)
requests
:
用于获取纯文本维基百科页面内容的小型 Python 代码