【问题标题】:Wikitravel XML Tree StructureWikitravel XML 树结构
【发布时间】:2012-06-29 01:43:26
【问题描述】:

我正在浏览 Wikitravel API,我注意到他们提供的 XML 文件只是将所有信息集中在一个大块中。示例:http://wikitravel.org/en/Special:Export/San_Francisco

是否有任何方法可以获取具有特定标题(例如,了解、进入、绕过等)的树?

【问题讨论】:

    标签: api parsing web-scraping mediawiki


    【解决方案1】:

    您可以使用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。
    • 如果您认为我的回答确实回答了您的问题,您应该accept it
    • @AnatoliyKukul 当我发布这个答案时它起作用了。您可能想尝试(由维基媒体支持的)分支 Wikivoyage:en.wikivoyage.org/w/…
    • @AnatoliyKukul 它仍然是相同的 API,我在原始答案中链接到它的描述。但恐怕没有简单的方法来获取气候信息表格,您必须解析相关页面的 wikitext 或 HTML。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2020-09-04
    • 2021-10-28
    • 1970-01-01
    • 2020-12-10
    • 2021-07-07
    • 1970-01-01
    相关资源
    最近更新 更多