如何从 Python 获取维基百科列表?
How to get Wikipedia lists from Python?
我正在尝试编写一个程序,从给定的专辑或配乐中获取歌曲列表。我一直在尝试通过使用 Wikipedia 来做到这一点,并且我发现了 2 个应该完成这项工作的模块:wikipedia and wikipedia-api。不幸的是,这些都没有得到我正在寻找的文章的元素,而是选择跳过它。显然,这对我一点帮助都没有。所以我想知道的是:是否有一个模块可以从维基百科文章中获取列表?还是我必须使用原始的维基百科 API 并自己创建一个?我试图避免获取原始 html 并对其进行解析,因为它会很复杂并且可能没有必要。
这里有一些您可以尝试的示例代码,它使用维基百科模块(pip install wikipedia),但两个模块的错误相同。
import wikipedia
article = wikipedia.search("civil war soundtrack")[0] # I've already checked this, the searching is definitely accurate and isn't the source of the problem
pg = wikipedia.page(article)
print(pg.text)
当它打印文本时,它不会打印 "Track listing" 部分下的 table,而是打印关于作曲家的行并完全跳过列表。
提前致谢!
注意:如果我没有立即回复,那是因为我睡着了,这里很晚,所以我早上再检查一下
注意2:如果我在提问的方式上有什么不对的地方,欢迎反馈!我过去曾因问题而被否决,但原因仍然让我困惑。
我尝试了以下库,其中 none 支持表格的提取:
- 维基百科
- 维基百科-api
- MediaWikiAPI
人们遇到了与您相同的问题,并且有人建议使用 MediaWikiAPI 和 Beautiful soup 解决您的问题:
# load page
mediawikiapi = MediaWikiAPI()
test_page = mediawikiapi.page(PageWithTables)
# scrape the HTML with BeautifulSoup to find tables
soup = BeautifulSoup(test_page.html(), 'html.parser')
tables = soup.findAll("table", { "class" : "wikitable" })
# select target table and apply custom function to export it to pandas
target_table = tables[0]
df_test = wikitable_to_dataframe(target_table)
我正在尝试编写一个程序,从给定的专辑或配乐中获取歌曲列表。我一直在尝试通过使用 Wikipedia 来做到这一点,并且我发现了 2 个应该完成这项工作的模块:wikipedia and wikipedia-api。不幸的是,这些都没有得到我正在寻找的文章的元素,而是选择跳过它。显然,这对我一点帮助都没有。所以我想知道的是:是否有一个模块可以从维基百科文章中获取列表?还是我必须使用原始的维基百科 API 并自己创建一个?我试图避免获取原始 html 并对其进行解析,因为它会很复杂并且可能没有必要。
这里有一些您可以尝试的示例代码,它使用维基百科模块(pip install wikipedia),但两个模块的错误相同。
import wikipedia
article = wikipedia.search("civil war soundtrack")[0] # I've already checked this, the searching is definitely accurate and isn't the source of the problem
pg = wikipedia.page(article)
print(pg.text)
当它打印文本时,它不会打印 "Track listing" 部分下的 table,而是打印关于作曲家的行并完全跳过列表。
提前致谢!
注意:如果我没有立即回复,那是因为我睡着了,这里很晚,所以我早上再检查一下
注意2:如果我在提问的方式上有什么不对的地方,欢迎反馈!我过去曾因问题而被否决,但原因仍然让我困惑。
我尝试了以下库,其中 none 支持表格的提取:
- 维基百科
- 维基百科-api
- MediaWikiAPI
人们遇到了与您相同的问题,并且有人建议使用 MediaWikiAPI 和 Beautiful soup 解决您的问题:
# load page
mediawikiapi = MediaWikiAPI()
test_page = mediawikiapi.page(PageWithTables)
# scrape the HTML with BeautifulSoup to find tables
soup = BeautifulSoup(test_page.html(), 'html.parser')
tables = soup.findAll("table", { "class" : "wikitable" })
# select target table and apply custom function to export it to pandas
target_table = tables[0]
df_test = wikitable_to_dataframe(target_table)