WebHarvest 常见问题

由于对xquery的语法不熟悉,导致debug折腾了两个晚上。遇到的问题基本可归纳成以下几点:

 

1. 中文字符编码问题

在抓取中文网页时,以下几个地方需要指定charset属性,否则会出现奇怪的错误

<?xml version="1.0" encoding="gb2312"?>

<file action="write" path="../data/plots.xml" charset="gb2312">

<http url="${sys.fullUrl(plot_top_url, url_plot)}" charset="gb2312"/>

 

2. xquery参数引用问题

引用外部的参数需要使用命名空间

declare variable $url as xs:string external;

 

另外,在写xquery过程中有两个FireBug工具可以使用:XPather, XqUSEme

 

XQuery教程

http://www.w3schools.com/xpath/xpath_functions.asp

 

3. 字符编码问题

当使用java -jar命令行参数执行时,如果配置文件里有中文字符,可能会出现XML解析出乱码。这是因为有一个全局配置参数,用来指定加载XML文件时的字符编码。可打开webHarvest窗口程序,在Execution->Setting菜单中设置File Enconding=GB2312,然后命令行运行就一切正常了。

 

4. 使用Kitchen调用函数时包含中文参数

当在脚本部署时使用Kitchen调用,脚本中引用到其它脚本文件里的函数,并且传递参数中出现中文字符时, 中文字符在编码时会出现问题,影响脚本的正常功能逻辑运行. 比如, 在一个脚本中调用common-function.xml里的download-multipage-list函数, 在调用时nextLinkUrl参数包含了中文'下一页', 使用kitchen发现有问题, 而在spoon中执行确是正常的. 这种情况下应当避免使用中文参数, 可改成其它xpath表达式.

 

以下是sample代码:

<?xml version="1.0" encoding="UTF-8"?>

<!--
    Expects following initial variable:
        search - search expression
-->

<config charset="UTF-8">

    <include path="common-functions.xml"/>

    <!-- defines search keyword and start URL -->
    <var-def name="search" overwrite="false">地球</var-def>
   
    <var-def name="url">
        <xpath expression="//noscript//a/@href[1]">
            <html-to-xml>
                <http url="http://images.google.com/images?q=地球&amp;hl=en&amp;btnG=Search+Images&amp;biw=1280&amp;bih=602"/>
            </html-to-xml>
        </xpath>
    </var-def>

    <!-- collects all image URLs -->
    <var-def name="imgLinks">
        <call name="download-multipage-list">
            <call-param name="pageUrl"><var name="url"/></call-param>
            <call-param name="nextXPath">//td[.='Next']/a/@href</call-param>
            <call-param name="itemXPath">//img[contains(@src, 'images?q=tbn')]/@src</call-param>
            <call-param name="maxloops">5</call-param>
        </call>
    </var-def>

    <!-- download images and saves them to the files -->
    <loop item="link" index="i" filter="unique">
        <list>
            <var name="imgLinks"/>
        </list>
        <body>
            <file action="write" type="binary" path="google_images/${search}_${i}.gif">
                <http url="${sys.fullUrl(url, link)}"/>
            </file>
        </body>
    </loop>

</config>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值