3

有没有办法从 MediaWikia 的 API 中提取部分文本?例如,此链接将所有内容转储为 XML 格式:

http://marvel.wikia.com/api.php?action=query&prop=revisions&titles=All-New%20X-Men%20Vol%201%201&rvprop=content&format=xml 

但是它没有太多的结构,即使是 json 格式。

我想获取 , 等的文本Writer1_1Penciler1_1也许我的参数不正确,所以也许还有其他选项可以输出。

您可以在此处以更易于阅读的方式查看内容。

4

1 回答 1

1

我确信正则表达式和最终拆分可能会更有效,但这可以完成您所要求的工作。

import urllib2
import re
data = urllib2.urlopen('http://marvel.wikia.com/api.php?action=query&prop=revisions&titles=All-New%20X-Men%20Vol%201%201&rvprop=content')
regex = re.compile('(Writer1_1|Penciler1_1)')
for line in data.read().split('|'):
    if regex.search(line):
        #assume everything after = is the full name
        print ' '.join(line.split()[2:])
于 2013-01-03T18:09:31.713 回答