Wikipedia API JSON和Python

问题描述 投票:2回答:2

我想从Wikipedia API调用的JSON文件中列出Vincent van Gogh的所有绘画的Python列表。这是我用来发出请求的网址:

http://en.wikipedia.org/w/api.php?format=json&action=query&titles=list%20of%20works%20by%20Vincent%20van%20Gogh&Page&prop=revisions&rvprop=content

正如您所看到的,如果您在浏览器中打开URL,那是一大堆文本。如何开始从大量的JSON返回中提取绘画的标题?在问这个问题之前,我已经做了大量研究,并尝试了许多方法来解决它。如果此JSON文件是一个有用的字典,将很有帮助,但我无法理解。您如何从该JSON文件中提取绘画名称?

python json api wikipedia
2个回答
4
投票

不是直接解析JSON API调用的结果,而是使用python wrapper

import wikipedia

page = wikipedia.page("List_of_works_by_Vincent_van_Gogh")
print page.links

也有other clients and wrappers

或者,这是使用BeautifulSoup HTML解析器的选项:

BeautifulSoup

0
投票

这里是将列表包含在熊猫数据框中的快速方法

>>> from bs4 import BeautifulSoup
>>> url = "http://en.wikipedia.org/wiki/List_of_works_by_Vincent_van_Gogh"
>>> soup = BeautifulSoup(urlopen(url))
>>> table = soup.find('table', class_="wikitable")
>>> for row in table.find_all('tr')[1:]:
...     print(row.find_all('td')[1].text)
... 
Still Life with Cabbage and Clogs
Crouching Boy with Sickle, Black chalk and watercolor
Woman Sewing, Watercolor
Woman with White Shawl
...
© www.soinside.com 2019 - 2024. All rights reserved.