如何通过维基百科 API 获取特定部分的文本

2024-01-06

我只想从维基百科页面中提取特定部分:

例子: 我想从维基百科文章“House”的“Parts”部分中提取文本。

https://en.wikipedia.org/wiki/House https://en.wikipedia.org/wiki/House

结果文本将是:

Many houses have several large rooms  .....  sections of the home (including in more recent eras a garage). 

我们可以从如下文章中获取洞文本:

但如何获取特定部分的文本呢?


您是否需要纯文本 wiki 文本或解析器生成的 HTML?

下面的示例为您提供了“布局”部分(内部文章的第三部分,您也可以使用任何其他部分 ID)。

当你想检索特定部分的已解析 html 时,你应该使用 parse api:或者,作为沙箱外部的 API 请求:

如果您想要特定部分的 wikitext,只需使用 wikitext 属性而不是 text 属性:

为了知道哪个部分有什么索引,您可以使用“sections”属性查询此信息,而不需要任何部分索引:

因此,作为仅使用 API 检索布局部分文本的完整示例,您将:

  1. 检索文章的章节:

回复:

{
    "parse": {
        "title": "House",
        "pageid": 13590,
        "sections": [
            {
                "toclevel": 1,
                "level": "2",
                "line": "Etymology",
                "number": "1",
                "index": "1",
                "fromtitle": "House",
                "byteoffset": 3549,
                "anchor": "Etymology"
            },
            {
                "toclevel": 1,
                "level": "2",
                "line": "Elements",
                "number": "2",
                "index": "2",
                "fromtitle": "House",
                "byteoffset": 4960,
                "anchor": "Elements"
            },
            {
                "toclevel": 2,
                "level": "3",
                "line": "Layout",
                "number": "2.1",
                "index": "3",
                "fromtitle": "House",
                "byteoffset": 4976,
                "anchor": "Layout"
            },
            {
                "toclevel": 2,
                "level": "3",
                "line": "Parts",
                "number": "2.2",
                "index": "4",
                "fromtitle": "House",
                "byteoffset": 6432,
                "anchor": "Parts"
            },
            {
                "toclevel": 2,
                "level": "3",
                "line": "History of the interior",
                "number": "2.3",
                "index": "5",
                "fromtitle": "House",
                "byteoffset": 7539,
                "anchor": "History_of_the_interior"
            },
            {
                "toclevel": 3,
                "level": "4",
                "line": "Communal rooms",
                "number": "2.3.1",
                "index": "6",
                "fromtitle": "House",
                "byteoffset": 8786,
                "anchor": "Communal_rooms"
            },
            {
                "toclevel": 3,
                "level": "4",
                "line": "Interconnecting rooms",
                "number": "2.3.2",
                "index": "7",
                "fromtitle": "House",
                "byteoffset": 9736,
                "anchor": "Interconnecting_rooms"
            },
            {
                "toclevel": 3,
                "level": "4",
                "line": "Corridor",
                "number": "2.3.3",
                "index": "8",
                "fromtitle": "House",
                "byteoffset": 11126,
                "anchor": "Corridor"
            },
            {
                "toclevel": 3,
                "level": "4",
                "line": "Employment-free house",
                "number": "2.3.4",
                "index": "9",
                "fromtitle": "House",
                "byteoffset": 13092,
                "anchor": "Employment-free_house"
            },
            {
                "toclevel": 2,
                "level": "3",
                "line": "Work location, technology and doctors",
                "number": "2.4",
                "index": "10",
                "fromtitle": "House",
                "byteoffset": 15969,
                "anchor": "Work_location,_technology_and_doctors"
            },
            {
                "toclevel": 3,
                "level": "4",
                "line": "Technology and privacy",
                "number": "2.4.1",
                "index": "11",
                "fromtitle": "House",
                "byteoffset": 17291,
                "anchor": "Technology_and_privacy"
            },
            {
                "toclevel": 1,
                "level": "2",
                "line": "Construction",
                "number": "3",
                "index": "12",
                "fromtitle": "House",
                "byteoffset": 18782,
                "anchor": "Construction"
            },
            {
                "toclevel": 2,
                "level": "3",
                "line": "Energy efficiency",
                "number": "3.1",
                "index": "13",
                "fromtitle": "House",
                "byteoffset": 21899,
                "anchor": "Energy_efficiency"
            },
            {
                "toclevel": 2,
                "level": "3",
                "line": "Earthquake protection",
                "number": "3.2",
                "index": "14",
                "fromtitle": "House",
                "byteoffset": 23057,
                "anchor": "Earthquake_protection"
            },
            {
                "toclevel": 1,
                "level": "2",
                "line": "Found materials",
                "number": "4",
                "index": "15",
                "fromtitle": "House",
                "byteoffset": 25172,
                "anchor": "Found_materials"
            },
            {
                "toclevel": 1,
                "level": "2",
                "line": "Legal issues",
                "number": "5",
                "index": "16",
                "fromtitle": "House",
                "byteoffset": 26235,
                "anchor": "Legal_issues"
            },
            {
                "toclevel": 2,
                "level": "3",
                "line": "United Kingdom",
                "number": "5.1",
                "index": "17",
                "fromtitle": "House",
                "byteoffset": 26644,
                "anchor": "United_Kingdom"
            },
            {
                "toclevel": 1,
                "level": "2",
                "line": "Identifying houses",
                "number": "6",
                "index": "18",
                "fromtitle": "House",
                "byteoffset": 26922,
                "anchor": "Identifying_houses"
            },
            {
                "toclevel": 1,
                "level": "2",
                "line": "Animal houses",
                "number": "7",
                "index": "19",
                "fromtitle": "House",
                "byteoffset": 27397,
                "anchor": "Animal_houses"
            },
            {
                "toclevel": 1,
                "level": "2",
                "line": "Houses and symbolism",
                "number": "8",
                "index": "20",
                "fromtitle": "House",
                "byteoffset": 27826,
                "anchor": "Houses_and_symbolism"
            },
            {
                "toclevel": 1,
                "level": "2",
                "line": "See also",
                "number": "9",
                "index": "21",
                "fromtitle": "House",
                "byteoffset": 28620,
                "anchor": "See_also"
            },
            {
                "toclevel": 1,
                "level": "2",
                "line": "References",
                "number": "10",
                "index": "22",
                "fromtitle": "House",
                "byteoffset": 29690,
                "anchor": "References"
            },
            {
                "toclevel": 1,
                "level": "2",
                "line": "External links",
                "number": "11",
                "index": "23",
                "fromtitle": "House",
                "byteoffset": 29720,
                "anchor": "External_links"
            }
        ]
    }
}
  1. 迭代结果并找到您想要的部分,检索索引
  2. 在下一个 API 请求中使用索引来获取该部分内容:

回复:

{
    "parse": {
        "title": "House",
        "pageid": 13590,
        "wikitext": {
            "*": "=== Layout ===\n[[File:Gingerbread House Essex CT.jpg|thumb|Example of an early [[Victorian architecture|Victorian]] \"Gingerbread House\" in [[Connecticut]], United States, built in 1855]]\n\nIdeally, [[architect]]s of houses design [[room]]s to meet the needs of the people who will live in the house. [[Feng shui]], originally a [[China|Chinese]] method of moving houses according to such factors as rain and micro-climates, has recently expanded its scope to address the design of interior spaces, with a view to promoting harmonious effects on the people living inside the house, although no actual effect has ever been demonstrated. Feng shui can also mean the \"aura\" in or around a dwelling, making it comparable to the [[real estate|real-estate]] sales concept of \"indoor-outdoor flow\".\n\nThe [[square footage]] of a house in the United States reports the area of \"living space\", excluding the garage and other non-living spaces. The \"square metres\" figure of a house in Europe <!-- including Malta ? --> reports the area of the walls enclosing the home, and thus includes any attached garage and non-living spaces.<ref>{{Cite book|title=Land Management: Challenges and Strategies (First Edition)|last=Iyyer|first=Chaitanya|publisher=Global India Publications Pvt Ltd|year=2009|isbn=978-9380228488|location=|pages=}}</ref>{{Citation needed|date=February 2007}} The number of floors or levels making up the house can affect the square footage of a home."
        }
    }
}

背景: 页面中的部分的想法尚未集成到修订中,修订“只是”整个页面的内容和附加元数据(例如在多个其他插槽中),但部分是内容的一部分(这是仅修订版中的一个位置)。这就是为什么当使用修订查询 API 时,您只能获取整个文本。需要解析页面才能知道节是什么,因为节是维基文本的概念,因此涉及解析器。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过维基百科 API 获取特定部分的文本 的相关文章

  • PHP 警告:PHP 启动:无法加载动态库“...”无法从共享对象映射段:无法在第 0 行的未知中分配内存

    我在我的开发机器上收到以下信息 PHP Warning PHP Startup Unable to load dynamic library usr lib php5 20100525 gd so usr lib php5 20100525
  • 从 MediaWiki XML 中提取页面标题和贡献者

    我有一个非常大 7GB 的 MediaWiki XML 转储 其中包含对 Wiki 每个页面所做的每次更改的记录 我试图记录哪些用户对每个页面做出了贡献 因此我想从 XML 中提取该信息 XML 看起来像
  • MediaWiki,尝试调查 VisualEditor“联系 Parsoid/RESTBase 服务器时出错 (HTTP 500)”

    很多人由于多种不同的原因而遇到相同的错误 因此 在尝试作曲家更新后 我尝试从浏览器开始调查它并逐步重建正在发生的事情 这对我来说真的很难 我非常感谢 PHP 开发人员的帮助 From Chrome Developer Tools I cou
  • 如何在Python中对维基百科类别进行分组?

    对于我的数据集的每个概念 我都存储了相应的维基百科类别 例如 考虑以下 5 个概念及其相应的维基百科类别 高甘油三酯血症 Category Lipid metabolism disorders Category Medical condit
  • 如何获取维基百科页面的所有链接及其维基数据 ID?

    什么时候 以下的事情可能实现 在单个查询 API 调用中获取维基百科页面上的所有链接及其各自的维基数据 ID 的列表 通过查询接收各个维基数据项的附加信息 例如属性值 要获取您必须使用的所有维基百科页面链接维基百科API https en
  • 我如何强制 MediaWiki 尊重单行中断?

    默认情况下 MediaWiki 会忽略单个换行符 将它们视为单个空格 要在页面中插入新行 必须在源代码中插入两个连续的换行符 这会产生一个新段落 p 也可以插入 br 直接进入源码 如何修改 MediaWiki 解析引擎 以便源代码中的单行
  • 如何在侧边栏中添加带有链接的图像 - MediaWiki

    我可以通过这种方式添加 MediaWiki 侧边栏的链接 some url url text 但是 如何在不破坏核心或标准模板的情况下添加图像而不是文本呢 我读过这个 http www mediawiki org wiki Manual t
  • 多流维基百科转储

    我下载了德语维基百科转储 dewiki 20151102 pages articles multistream xml 我的简短问题是 在这种情况下 多流 意味着什么 转储使用 bz2 进行压缩 bz2 支持并行版本 使其能够更快地压缩 解
  • 单个查询中的 Wikipedia API 和 SPARQL

    我需要搜索其中包含一些特定单词的维基百科页面全文 为了改善结果 我想将结果限制为描述实体的页面的实例一个特定的实体 为了搜索全文 我可以使用 Wikipedia API 使用查询动作和搜索生成器 为了过滤给定实体的实例 我可以使用 Wiki
  • 从 mediawiki api ios 过滤数据

    我使用 action query prop revisions rvprop content keywords format json redirects API 来获取有关 Anil Ambani 的详细信息 作为回应 我得到了以下内容字
  • 如何从维基百科中获取纯文本

    我想编写一个仅获取维基百科描述部分的脚本 也就是说 当我说 wiki bla bla bla 它将转到维基百科页面为bla bla bla http en wikipedia org wiki Bla Bla Bla 获取以下内容 并将其返
  • 从 MediaWiki 操作 URL 中删除 index.php

    好的 所以它相当有据可查 https stackoverflow com questions 6667847 mediawiki removing index php from url如何隐藏 url 中的 index php title
  • 如何在不注册机器人帐户的情况下以编程方式从维基共享资源下载图像?

    机器人帐户获得批准的唯一方法似乎是添加或编辑维基媒体上已有的信息 如果您尝试在没有机器人帐户的情况下使用某些 api 库下载任何图像 您会收到错误消息而不是图像 似乎他们阻止了任何不从浏览器进入的人 其他人有这方面的经验吗 我在这里错过了什
  • 维基百科Python API

    我正在尝试使用 Python 的维基百科 API 查看维基百科页面中的目录 这是我的代码 gt gt gt import wikipedia gt gt gt ny wikipedia page New York gt gt gt ny s
  • 注册媒体维基需要管理员批准吗?

    我维护的一个 wiki 受到了垃圾邮件机器人的严重打击 我们没有很多用户 而且我不想让合法用户背负验证码 有没有一种简单的方法可以让管理员确认注册 我浏览了手册 但无法弄清楚如何操作 您可以创建一个新用户权限 例如 批准 允许管理员分配该权
  • 维基百科文本下载

    我正在寻找为我的大学项目下载完整的维基百科文本 我是否必须编写自己的蜘蛛才能下载此内容 或者是否有在线维基百科的公共数据集 为了给你一些我的项目的概述 我想找出我感兴趣的几篇文章中有趣的单词 但是为了找到这些有趣的单词 我计划应用 tf i
  • 将 ipython 笔记本转换为 mediawiki

    我想将 ipython 笔记本转换为 mediawiki 标记 我有两个想法如何做到这一点 自定义导出nbconvert tool 先导出为LaTeX 然后使用pandoc将其转换为 mediawiki 标记 我在第一个选项中找不到任何内容
  • 使用 MediaWiki 从 Wikia 页面提取文本,但返回结果一片混乱,是否有更好的方法可以从每个部分提取文本?

    我正在开发一个 Android 应用程序 它从 Wikia 页面提取信息并将其显示在应用程序中 我目前正在拉动所有类别进行导航 并将我的应用程序设置为在 WebView 中显示页面 但我只想拉动信息并自行格式化 而不是通过传递到 WebVi
  • 维基百科与 Python

    我有这个非常简单的 python 代码来读取 wikipedia api 的 xml import urllib from xml dom import minidom usock urllib urlopen http en wikipe
  • 使用 Python 抓取维基百科数据

    我正在尝试从以下内容中检索 3 列 NFL 球队 球员姓名 大学球队 维基百科页面 http en wikipedia org wiki 2008 NFL draft 我是 python 新手 一直在尝试使用 beautifulsoup 来

随机推荐