【发布时间】:2012-06-29 01:43:26
【问题描述】:
我正在浏览 Wikitravel API,我注意到他们提供的 XML 文件只是将所有信息集中在一个大块中。示例:http://wikitravel.org/en/Special:Export/San_Francisco
是否有任何方法可以获取具有特定标题(例如,了解、进入、绕过等)的树?
【问题讨论】:
标签: api parsing web-scraping mediawiki
我正在浏览 Wikitravel API,我注意到他们提供的 XML 文件只是将所有信息集中在一个大块中。示例:http://wikitravel.org/en/Special:Export/San_Francisco
是否有任何方法可以获取具有特定标题(例如,了解、进入、绕过等)的树?
【问题讨论】:
标签: api parsing web-scraping mediawiki
您可以使用MediaWiki API 中的action=parse 来执行此操作。
例如,查询 http://wikitravel.org/wiki/en/api.php?format=xml&action=parse&prop=sections&page=San%20Francisco 将返回如下内容:
<api>
<parse>
<sections>
<s toclevel="1" level="2" line="Districts" number="1" index="1" fromtitle="San_Francisco" byteoffset="1186" anchor="Districts"/>
<s toclevel="1" level="2" line="Understand" number="2" index="2" fromtitle="San_Francisco" byteoffset="9563" anchor="Understand"/>
<s toclevel="2" level="3" line="History" number="2.1" index="3" fromtitle="San_Francisco" byteoffset="9578" anchor="History"/>
<s toclevel="2" level="3" line="Climate" number="2.2" index="4" fromtitle="San_Francisco" byteoffset="13913" anchor="Climate"/>
<s toclevel="2" level="3" line="Literature" number="2.3" index="5" fromtitle="San_Francisco" byteoffset="16502" anchor="Literature"/>
<s toclevel="2" level="3" line="Movies" number="2.4" index="6" fromtitle="San_Francisco" byteoffset="19404" anchor="Movies"/>
<s toclevel="2" level="3" line="Tourist information" number="2.5" index="7" fromtitle="San_Francisco" byteoffset="23236" anchor="Tourist_information"/>
<s toclevel="1" level="2" line="Talk" number="3" index="8" fromtitle="San_Francisco" byteoffset="24227" anchor="Talk"/>
…
</sections>
</parse>
</api>
由此,您可以重建节树。
【讨论】:
prop=sections|wikitext获取原文。文本不会分成多个部分,但您应该可以使用 byteoffsets。