zorba3.0的编译安装与python binding的使用

    xiaoxiao2021-03-26  42

    自己的项目必须用一个全功能的xquery引擎,并要在python中调用,找了很久只找到了zorba,但是xquery本身非常冷门,zorba也是找不到用武之地,活跃度非常小,导致文档的结构与排版非常奇葩而且文档和源码有两处错误,再加上zorba本身需要很多的第三方包支持,我第一次尝试编译zorba的时候基本毫无头绪。但是没有办法,市面上的xquery就它一个,非他不可,所以只能头硬死磕,终于在解决所有依赖又找到两处错误后成功安装了zorba。现在分享出来,如果你也要安zorba,这篇文章能给你省下几天的时间。

    1.安装

    依赖的源码包都是在官网下载,版本不都是最新的,没有测试过任何依赖版本兼容问题,只知道这一套依赖可以成功。下面是在ubuntu 12上的伪命令,因为做总结时已经是第二次安装了,依赖包是拷过来的,所以没有写完整的url,另外系统可能也比较老了,请见谅。

    sudo apg-get install cmake wget libiconv-1.14.tar.gz tar -zxvf libiconv-1.14.tar.gz ./configure make sudo make install wget libuuid-1.0.3.tar.gz tar -zxvf libuuid-1.0.3.tar.gz ./configure make sudo make install wget libxml2-2.7.2.tar.gz tar -zxvf libxml2-2.7.2.tar.gz ./configure make sudo make install wget libxslt-git-snapshot.tar.gz tar -zxvf libxslt-git-snapshot.tar.gz ./configure make sudo make install wget swig-2.0.4.tar.gz tar -zxvf swig-2.0.4.tar.gz ./configure make sudo make install wget tidy-5.2.0-64bit.deb sudo dpkg -i tidy-5.2.0-64bit.deb wget xerces-c-3.1.4.zip unzip -q xerces-c-3.1.4.zip ./configure make sudo make install wget zorba-3.0.tar.gz tar -zxvf zorba-3.0.tar.gz cd zorba-3.0/ mkdir build cd build/ cmake -D ZORBA_SUPPRESS_SWIG=OFF .. #这里官网文档上标注的是-D ZORBA_USE_SWIG=ON查了cmake配置才发现变量名已经改了,而且意义也相反 make vim {zorba_build_dir}/swig/python/cmake_install.cmake #第40行,将{...}/build/swig/python/_zorba_api_python.so改为{...}/build/swig/python/_zorba_api.so sudo make install

    经过上面的步骤zorba就可以使用了,在命令行上打zorba 就会看到相应的提示

    $ zorba Error: No queries submitted Usage: zorba -q '1 + 1' execute an inline query zorba file.xq execute a query from a file Use -h for help.

    然后进入到{zorba_build_dir}/swig/python/目录下,在进入python交互界面,就可以引用zorba的python模块了:

    $ cd {zorba_build_dir}/swig/python/ $ python >>>import zorba_api >>>

    2.python binding的使用

    test.py:

    #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on Aug 7, 2016 @author: finetu ''' import zorba_api from bs4 import BeautifulSoup soup = BeautifulSoup(open("index2.html"),"lxml") # print str(soup.prettify()) all_the_text = soup.prettify().encode("utf8") store = zorba_api.InMemoryStore_getInstance() zorba = zorba_api.Zorba_getInstance(store) dataManager = zorba.getXmlDataManager() docIter = dataManager.parseXML(all_the_text) docIter.open(); doc = zorba_api.Item_createEmptyItem() docIter.next(doc) docIter.close() docIter.destroy() docManager = dataManager.getDocumentManager() docManager.put("response", doc) xquery = zorba.compileQuery( """ for $_postDiv in doc('response')//div[@class='post f list-post'] let $_postTitle := {data($_postDiv/div[@class='indexs']/h2/a)}, $_postHref := {data($_postDiv/div[@class='indexs']/h2/a/@href)}, $_postAuthor := {data($_postDiv/div[@class='indexs']/div[@class='time_s']/a)} return <item> <title>{$_postTitle}</title> <href>{$_postHref}</href> <author>{$_postAuthor}</author> </item> """) dynCtx = xquery.getDynamicContext(); dynCtx.setContextItem(doc); print xquery.execute()

    index2.html

    <div id="content">     <h3 class="title"><a href="http://jandan.net/2016/09/02" title="2016年9月02日 ">今天</a> · <a href="http://jandan.net/2016/09/01">昨天</a> </h3>     <div class="post f list-post">      <div class="thumbs_b">      <a href="http://jandan.net/2016/09/01/temporal-discounting.html" target="_blank"><img class="lazy" data-original="//tankr.net/s/custom/STVK.jpg" width="175" height="98" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" /></a>     </div>      <div class="indexs">       <span class="comment-link" title="20 hours ago">24</span>       <h2><a href="http://jandan.net/2016/09/01/temporal-discounting.html" target="_blank">让你变穷的「时间贴现」</a></h2>       <div class="time_s">       <a href="http://jandan.net/author/Miriam">蛋奶</a> ·        <strong><a href="http://jandan.net/tag/小贴士" rel="tag">小贴士</a></strong>       </div> 相比起明天才能看到的钱,人们更享受今天花钱带来的快感。       <a id="jandan-zan-81990" class="jandan-zan" οnclick="jandan_zan(81990)" href="javascript:;"> <span class="zan-icon">⊙8</span> </a>      </div>     </div>     <div class="post f list-post">      <div class="thumbs_b">      <a href="http://jandan.net/2016/09/01/egyptians-strategy.html" target="_blank"><img class="lazy" data-original="//tankr.net/s/custom/EHJE.jpg" width="175" height="98" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" /></a>     </div>      <div class="indexs">       <span class="comment-link" title="21 hours ago">51</span>       <h2><a href="http://jandan.net/2016/09/01/egyptians-strategy.html" target="_blank">古埃及人告诉你如何应对强词夺理的上司</a></h2>       <div class="time_s">       <a href="http://jandan.net/author/Miriam">蛋奶</a> ·        <strong><a href="http://jandan.net/tag/考古" rel="tag">考古</a></strong>       </div> 4000年的古埃及箴言仍不过时。       <a id="jandan-zan-81983" class="jandan-zan" οnclick="jandan_zan(81983)" href="javascript:;"> <span class="zan-icon">⊙20</span> </a>      </div>     </div>    </div> 运行test.py

    $ python test.py <?xml version="1.0" encoding="UTF-8"?> <item><title> 让你变穷的「时间贴现」 </title><href>http://jandan.net/2016/09/01/temporal-discounting.html</href><author> 蛋奶 </author></item><item><title> 古埃及人告诉你如何应对强词夺理的上司 </title><href>http://jandan.net/2016/09/01/egyptians-strategy.html</href><author> 蛋奶 </author></item>

    python 模块的使用也非常蹩脚,可能是没有领会到精髓,貌似zorba必须从一个docManager 对象里面读取内容,重复使用时要注意把docmanager清空。但是无论如何它是个全功能的xquery引擎,和JSONiq引擎,对于需要这种工具的开发者来说都是心怀感激。

    转载请注明出处:http://blog.csdn.net/gklifg/article/details/54288555

    转载请注明原文地址: https://ju.6miu.com/read-500205.html

    最新回复(0)