如何使用维基百科的 URL 获取维基百科内容?

How to get Wikipedia content using Wikipedia's URL?

是否可以创建一个输入字段,您可以在其中粘贴维基百科页面 link 并且它会从该页面获取所有文本内容?

我正在尝试在我的 Web 应用程序中集成一项功能,人们可以将他们想要分析的维基百科页面 link/URL 粘贴到输入字段中。应用程序将使用 URL 从该页面获取所有文本内容。

假设用户输入此 link:https://en.wikipedia.org/wiki/Taylor_Swift

应用程序将return该页面的文本内容,如下所示:

Taylor Alison Swift (born December 13, 1989) is an American singer-songwriter. Her narrative songwriting, which often centers around her personal life, has received widespread media coverage. Born in West Reading, Pennsylvania, Swift relocated to Nashville, Tennessee in 2004 to pursue a career in country music. At age 14, she became the youngest artist signed by the Sony/ATV Music publishing house, and at age 15, she signed her first record deal. Her 2006 eponymous debut studio album was the longest-charting album of the 2000s on the Billboard 200. Its third single, "Our Song", made her the youngest .......

此外,我试过这个 api,它有效,但它只是 return 的 header 内容,而不是整个页面内容

我浏览了 Wikipedia API 并找到了 none(还)。关于如何执行此操作有什么建议吗?

由于您在问题中标记了 node.js,我假设您使用的是 Javascript。您可以使用名为 wikijs

的 npm 库

来自 wikijs 页面的示例

wiki({ apiUrl: 'https://es.wikipedia.org/w/api.php' })
    .page('Cristiano Ronaldo')
    .then(page => page.info())
    .then(console.log);

希望这对你有用

您可以使用 Mediawiki 中的 API 获取文章的任何格式的文本:

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

实际上和你在问题中提到的 API 是一样的,但唯一的区别是你应该删除 &exintro 参数,并添加这两个参数来代替 &exlimit=max&explaintext