How to extract data from a Wikipedia article?

∥☆過路亽.° 提交于 2019-12-02 21:20:27

Unfortunatelly, it seems the mediawiki.org documentation for parse doesn't tell you how to do this. But the documentation in the API itself does: You can use section parameter. And you can use prop=sections to get the list of sections.

So, you could first use:

http://en.wikipedia.org/w/api.php?format=xml&action=parse&page=Android_%28operating_system%29&prop=sections

to get the list of sections and then

http://en.wikipedia.org/w/api.php?format=xml&action=parse&page=Android_%28operating_system%29&prop=text&section=26

to get the HTML for a certain section.

MaxSem

action=parse doesn't work well with per-section parse, consider this shoert example:

Foo is a bar<ref>really!</ref>
== References ==
<references/>

Parsing just the zeroth section will result in red error message about without while parsing the first one will result in empty references list.

However, there's a better solution: action=mobileview is not only free from this problem, but it's also specifically intended for mobile apps and gives you mobile-optimized HTML.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!