如何从维基百科 API 获取表格中的数据? [英] How to obtain data in a table from Wikipedia API?

查看:29
本文介绍了如何从维基百科 API 获取表格中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从

解决方案

您可以通过使用 MediaWiki API 和 action=parse 分两步.首先,您必须使用以下命令从页面中获取所有部分:

https://en.wikipedia.org/w/api.php?action=parse&prop=sections&page=Wikipedia:Unusual_articles

从响应中您看到 Americas 部分具有 index=T-2(T 表示嵌入页面) 并且它来自 fromtitle=Wikipedia:Unusual_articles/Places_and_infrastructure.现在我们使用这些 indexfromtitle 来获取部分的内容:

https://en.wikipedia.org/w/api.php?action=parse&page=Wikipedia:Unusual_articles/Places_and_infrastructure&section=2&prop=...

哪里:

  • prop=wikitext - 给出被解析的原始部分 wikitext.
  • prop=text - 给出维基文本的解析部分文本.

I'm trying to get all the content from Wikipedia:Unusual_articles and I'm able to get the list of table content by calling this endpoint:

https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=sections&page=Wikipedia:Unusual_articles

and the data I got back look something like this:

{
    title: "Wikipedia:Unusual articles",
    pageid: 154126,
    sections: [
        {
            toclevel: 1,
            level: "2",
            line: "Places and infrastructure",
            number: "1",
            index: "T-1",
            fromtitle: "Wikipedia:Unusual_articles/Places_and_infrastructure",
            byteoffset: null,
            anchor: "Places_and_infrastructure"
        },
        {
            toclevel: 2,
            level: "3",
            line: "Americas",
            number: "1.1",
            index: "T-2",
            fromtitle: "Wikipedia:Unusual_articles/Places_and_infrastructure",
            byteoffset: null,
            anchor: "Americas"
        },
...

But I'm not able to get the content of a particular section. For example under Americas is a list of the table with a link and a short description, but is there a way to obtain the link and short description from the API?

解决方案

You can get the content of every page section by using MediaWiki API with action=parse in two steps. First you have to get all sections from the page with:

https://en.wikipedia.org/w/api.php?action=parse&prop=sections&page=Wikipedia:Unusual_articles

From the response you see that section Americas has index=T-2 (T means transcluded page) and it comes from fromtitle=Wikipedia:Unusual_articles/Places_and_infrastructure. Now we use these index and fromtitle to get the content of the section with:

https://en.wikipedia.org/w/api.php?action=parse&page=Wikipedia:Unusual_articles/Places_and_infrastructure&section=2&prop=...

where:

  • prop=wikitext - gives the original section wikitext that was parsed.
  • prop=text - gives the parsed section text of the wikitext.

这篇关于如何从维基百科 API 获取表格中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆