如何解析/从的mediawiki提取数据标记的通过Python文章 [英] How to parse/extract data from a mediawiki marked-up article via python

查看:187
本文介绍了如何解析/从的mediawiki提取数据标记的通过Python文章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href=\"http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Foobar&redirects&rvprop=content&format=xml\">Source Mediawiki的标记

现在,我使用各种正则表达式来分析,在MediaWiki的标记成列表/字典中的数据,使文章内的元素都可以使用。

Right now I'm using a variety of regexes to "parse" the data in the mediawiki mark-up into lists/dictionaries, so that elements within the article can be used.

这很难说是最好的方法,因为这必须作出的案件数量都很大。

This is hardly the best method, as the number of cases that have to be made are large.

一个人怎么会解析文章的链接到MediaWiki标记成各种Python对象,使之内可以使用的数据?

How would one parse an article's mediawiki markup into a variety of python objects so that the data within can be used?

例子是:


  • 提取所有的头条新闻到
    字典,用哈希处理的
    部分。

  • 抓住所有wiki内链接,并
    把它们粘到一个列表(我知道结果
    这可从API来完成,但我最好
    而只有一个API调用结果
    减少带宽使用)。

  • 提取所有图像名,并与他们的哈希
    他们的部分

一个正则表达式各种可以实现上述目标,但我发现我必须作出相当大的数量。

A variety of regexes can achieve the above, but I'm finding the number I have to make rather large.

这里的 MEDIAWIKI非官方规范(我不觉得他们的官方的specification 一样有用)。

Here's the mediawiki unofficial specification (I don't find their official specification as useful).

推荐答案

pedia preSS / mwlib

mwlib提供用于解析链接到MediaWiki物品,并将它们转换成不同的输出格式的库。 mwlib是为了产生维基百科的文章PDF文档使用维基百科的打印/导出功能。

mwlib provides a library for parsing MediaWiki articles and converting them to different output formats. mwlib is used by wikipedia's "Print/export" feature in order to generate PDF documents from wikipedia articles.

这里的文档页面。使用较老的文档页面有一个班轮例如:

Here's the documentation page. The older doc page used have a one-liner example:

from mwlib.uparser import simpleparse
simpleparse("=h1=\n*item 1\n*item2\n==h2==\nsome [[Link|caption]] there\n")

如果你想看看它是如何在行动中使用,请参阅测试用例来与code。 (<一href=\"https://github.com/pedia$p$pss/mwlib/blob/c41330f95e0e780026159ae95402b6d00511e05c/tests/test_parser.py\"相对=nofollow> mwlib /测试/从Git仓库 test_parser.py):

If you want to see how it's used in action, see the test cases that come with the code. (mwlib/tests/test_parser.py from git repository):

from mwlib import parser, expander, uparser
from mwlib.expander import DictDB
from mwlib.xfail import xfail
from mwlib.dummydb import DummyDB
from mwlib.refine import util, core

parse = uparser.simpleparse

def test_headings():
    r=parse(u"""
= 1 =
== 2 ==
= 3 =
""")

    sections = [x.children[0].asText().strip() for x in r.children if isinstance(x, parser.Section)]
    assert sections == [u"1", u"3"]

另请参见标记规范并的替代解析器了解详情。

这篇关于如何解析/从的mediawiki提取数据标记的通过Python文章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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