wikimedia api从json字符串获取相关数据

这是我昨天问的问题 。 我能够获得所需的数据。 最终数据是这样的。 请点击此链接 。

我尝试使用以下代码来获取所有信息框数据

content = content.split("}}\n"); for(k in content) { if(content[k].search("Infobox")==2) { var infobox = content[k]; alert(infobox); infobox = infobox.replace("{{",""); alert(infobox); infobox = infobox.split("\n|"); //alert(infobox[0]); var infohtml=""; for(l in infobox) { if(infobox[l].search("=")>0) { var line = infobox[l].split("="); infohtml = infohtml+""+line[0]+""+line[1]+""; } } infohtml=""+infohtml+"
"; $('#con').html(infohtml); break; } }

我最初认为每个元素都包含在{{}}中。 所以我写了这段代码。 但我所看到的是,我无法通过此获取整个信息框数据。 有这个元素

 {{Sfn|National Informatics Centre|2005}} 

发生,我的信息框数据结束。

没有使用json,它似乎要简单得多。 请帮我

你试过DBpedia吗? Afaik他们提供模板使用信息。 还有一个名为Templatetiger的工具服务器工具,它从静态转储(不是实时)中提取模板。

但是,我曾经在javascript中写了一个小片段来从wikitext中提取模板:

 var title; // of the template var wikitext; // of the page var templateRegexp = new RegExp("{{\\s*"+(title.indexOf(":")>-1?"(?:Vorlage:|Template:)?"+title:title)+"([^[\\]{}]*(?:{{[^{}]*}}|\\[?\\[[^[\\]]*\\]?\\])?[^[\\]{}]*)+}}", "g"); var paramRegexp = /\s*\|[^{}|]*?((?:{{[^{}]*}}|\[?\[[^[\]]*\]?\])?[^[\]{}|]*)*/g; wikitext.replace(templateRegexp, function(template){ // logabout(template, "input "); var parameters = template.match(paramRegexp); if (!parameters) { console.log(page.title + " ohne Parameter:\n" + template); parameters = []; } var unnamed = 1; var p = parameters.reduce(function(map, line) { line = line.replace(/^\s*\|/,""); var i = line.indexOf("="); map[line.substr(0,i).trim() || unnamed++] = line.substr(i+1).trim(); return map; }, {}); // you have an object "p" in here containing the template parameters }); 

它具有一级嵌套模板,但仍然非常容易出错。 使用正则表达式解析wiki文件与在html上尝试执行它一样邪恶:-)

从api查询解析树可能更容易: api.php?action = query&prop = revisions&rvprop = content& rvgeneratexml = 1 &titles = …. 从该分析树,您将能够轻松地提取模板。