问题
Within Infobox at wikipedia some attributes values are also inside curly braces {{}}.. Some time they have lins also.. I need values inside the braces, which is displayed on wikipedia web page. I read these are templates also.. Can anyone give me some link or guide me how do I deal with it?
回答1:
Double-curly-braces {{}}
define a call to some kind of magic word, variable, parser function, or template.. Help can be found on MediaWiki.org/.../Manual:Magic_words. The little lines that look like |
are called pipes and are used to as separators that allow the wikicore parsing engine to define parameters that can be used with the magic word, variable, parser function, or template..
回答2:
Hopefully this will help everyone who come across this very same issue. Considering you will be parsing the infobox with PHP, you can use this: http://www.mywiki.com/wiki/api.php?format=xml&action=query&titles=PAGE_TITLE_THAT_CONTAINS_AN_INFOBOX&prop=revisions&rvprop=content&rvgeneratexml=1
'rvgeneratexml' is being set to true (1), this will make the xml node <rev>
generate an attribute "parsetree" containing the infobox information in XML format.
Then, in PHP, you can load the whole information (<api>
everything including <rev></api>
) with simpleXML:
$xml = simplexml_load_file($url);
Then you can load the template's information by getting the "parsetree" attribute and loading the string with:
$template = simplexml_load_string($xml->query->pages->page->revisions->rev->attributes()->parsetree);
$template = $template->template; // If more than 1 template, check template[0], [1], etc
Then, by using the correct structure, you can access the elements with something like:
if ($template->part[0]->name='name')
$film = $template->part[0]->value;
Then, $film
will contain the film's name (->name
is the parameter's name, and ->value
is its value).
来源:https://stackoverflow.com/questions/10150131/how-to-parse-the-attributes-value-inside-curly-braces-inside-a-infobox