语义mediawiki python绑定
smw的Python项目详细描述
Sematic MediaWikipython绑定
此软件包取决于MWClient 0.7开发版本
https://github.com/mwclient/mwclient.git
安装(将自动安装MWClient 0.7dev)
pip install smw
或从github安装
pip install git+git://github.com/baojie/smw.git
用法示例
fromsmw.SemanticMediaWikiimportSemanticMediaWiki# change this to your wiki's configconfig={"host":"www.foo.com","path":"/wiki/","http_login":None,"http_pass":None,"wiki_login":None,"wiki_pass":None,}wiki=SemanticMediaWiki(host=config["host"],path=config["path"],http_login=config["http_login"],http_pass=config["http_pass"],wiki_login=config["wiki_login"],wiki_pass=config["wiki_pass"])# or from a config file (json)# default ~/.smwrc# sample config https://github.com/baojie/smw/blob/master/smwrc.samplewiki=SemanticMediaWiki.from_config()query=r""" {{#ask: [[SMW_PYTHON_TEST::+]] |?SMW_PYTHON_TEST |format = json }} """res=wiki.get_data(query,format='json')[query_result,query_path]=res# process query results from SMW >= 1.8.0items=query_result['results']forpageinitems:printitems[page]page_name='Foo'# get page metadata in RDF/XMLrdf=wiki.getRDF(page_name)# get page metadata in JSON, simplified for human readabilityjson_data=wiki.getJSON(page_name)# parse any wiki scriptresult=wiki.parse("'''Hello'''")# get raw page# this is useful when query is composed on Special:Askhtml=wiki.get('/wiki/Main_Page')json_result=wiki.get('/wiki/index.php?title=Special:Ask&q=[[Category:People]]&p[format]=json')# add semantic annotationspage=wiki.site.Pages['SnowWhite']old_content=page.edit()new_content=old_content+"[[Category:Princess]][[friend of::Grumpy]]"page.save(new_content)