2011-11-11 90 views
6

我需要獲取任何電影信息框的內容。我知道電影的名字。一種方法是獲取Wikipedia頁面的完整內容,然後解析它,直到找到{{Infobox,然後獲取信息框的內容。維基百科信息框的內容

是否有任何其他方式使用某些API或解析器相同?

我正在使用Python和pywikipediabot API。

我也很熟悉wikitools API。因此,如果有人提供與wikitools API相關的解決方案,而不是pywikipedia,請提及這一點。

+1

另請參見** [mediawiki api:如何從維基百科文章獲取信息框](http://stackoverflow.com/questions/7638402/mediawiki-api-how-to-get-infobox-from-a-wikipedia - 文章)**和** [獲取所有維基百科信息框模板和所有頁面使用它們](http://stackoverflow.com/questions/8000211/get-all-wikipedia-infobox-templates-and-all-pages-using -them)** – hippietrail

+0

你想得到模板包含字符串或解析結果嗎? – Bergi

回答

6

而不是重新發明輪子,檢查出DBPedia,它已經提取所有的維基百科信息框到一個易於分析的數據庫格式。

+4

我建議這個,但我的教授想讓我這樣做 –

+0

下面是一大堆MediaWiki解析器然後:http://www.mediawiki.org/wiki/Alternative_parsers – jpatokal

+0

@jpatokal但是,如何使用REST操作來獲取數據來自DBPedia的信息框。請你請請指出教程,資源,並給一些指導我怎麼能這樣做? –

0

您可以使用pywikipdiabot獲取wikipage內容,然後,您可以使用正則表達式,解析器(如mwlib [0])搜索信息框,甚至可以使用pywikipediabot進行搜索並使用他的模板工具之一。例如在textlib上,你會發現一些處理模板的函數(提示:搜索「#模板處理函數」)。 [1]

[0] - http://pypi.python.org/pypi/mwlib

[1] - http://svn.wikimedia.org/viewvc/pywikipedia/trunk/pywikipedia/pywikibot/textlib.py?view=markup

7

另一個巨大鏈接到MediaWiki解析器mwparserfromhell

In [1]: import mwparserfromhell 

In [2]: import pywikibot 

In [3]: enwp = pywikibot.Site('en','wikipedia') 

In [4]: page = pywikibot.Page(enwp, 'Waking Life')    

In [5]: wikitext = page.get()    

In [6]: wikicode = mwparserfromhell.parse(wikitext) 

In [7]: templates = wikicode.filter_templates() 

In [8]: templates? 
Type:  list 
String Form:[u'{{Use mdy dates|date=September 2012}}', u"{{Infobox film\n| name   = Waking Life\n| im <...> critic film|waking-life|Waking Life}}', u'{{Richard Linklater}}', u'{{DEFAULTSORT:Waking Life}}'] 
Length:  31 
Docstring: 
list() -> new empty list 
list(iterable) -> new list initialized from iterable's items 

In [10]: templates[:2] 
Out[10]: 
[u'{{Use mdy dates|date=September 2012}}', 
u"{{Infobox film\n| name   = Waking Life\n| image   = Waking-Life-Poster.jpg\n| image_size  = 220px\n| alt   =\n| caption  = Theatrical release poster\n| director  = [[Richard Linklater]]\n| producer  = [[Tommy Pallotta]]<br />[[Jonah Smith]]<br />Anne Walker-McBay<br />Palmer West\n| writer   = Richard Linklater\n| starring  = [[Wiley Wiggins]]\n| music   = Glover Gill\n| cinematography = Richard Linklater<br />[[Tommy Pallotta]]\n| editing  = Sandra Adair\n| studio   = [[Thousand Words]]\n| distributor = [[Fox Searchlight Pictures]]\n| released  = {{Film date|2001|01|23|[[Sundance Film Festival|Sundance]]|2001|10|19|United States}}\n| runtime  = 101 minutes<!--Theatrical runtime: 100:40--><ref>{{cite web |title=''WAKING LIFE'' (15) |url=http://www.bbfc.co.uk/releases/waking-life-2002-3|work=[[British Board of Film Classification]]|date=September 19, 2001|accessdate=May 6, 2013}}</ref>\n| country  = United States\n| language  = English\n| budget   =\n| gross   = $3,176,880<ref>{{cite web|title=''Waking Life'' (2001)|work=[[Box Office Mojo]] |url=http://www.boxofficemojo.com/movies/?id=wakinglife.htm|accessdate=March 20, 2010}}</ref>\n}}"] 

In [11]: infobox_film = templates[1] 

In [12]: for param in infobox_film.params: 
      print param.name, param.value 

name    Waking Life 

image   Waking-Life-Poster.jpg 

image_size  220px 

alt    

caption   Theatrical release poster 

director   [[Richard Linklater]] 

producer   [[Tommy Pallotta]]<br />[[Jonah Smith]]<br />Anne Walker-McBay<br />Palmer West 

writer   Richard Linklater 

starring   [[Wiley Wiggins]] 

music   Glover Gill 

cinematography Richard Linklater<br />[[Tommy Pallotta]] 

editing   Sandra Adair 

studio   [[Thousand Words]] 

distributor  [[Fox Searchlight Pictures]] 

released   {{Film date|2001|01|23|[[Sundance Film Festival|Sundance]]|2001|10|19|United States}} 

runtime   101 minutes<!--Theatrical runtime: 100:40--><ref>{{cite web |title=''WAKING LIFE'' (15) |url=http://www.bbfc.co.uk/releases/waking-life-2002-3|work=[[British Board of Film Classification]]|date=September 19, 2001|accessdate=May 6, 2013}}</ref> 

country   United States 

language   English 

budget   

gross   $3,176,880<ref>{{cite web|title=''Waking Life'' (2001)|work=[[Box Office Mojo]] |url=http://www.boxofficemojo.com/movies/?id=wakinglife.htm|accessdate=March 20, 2010}}</ref> 

不要忘了params也是mwparserfromhell對象!

相關問題