开发者社区> 问答> 正文

采集一个简单的页面,我配置的分页一直不对,只能采集第一页?报错

以下是我配置的XML文件:

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


<task>


<!--  timeOutSecond:抓取时请求超时时间(单位:秒)
     errorRetry:抓取失败重连次数
     errorDelayTime: 抓取失败后等待时间
 fetchPrepareDelay:抓取前延迟时间(单位:秒),防止对某个网站太频繁爬
 runThreadNum:  线程数
 charset:编码  (如果设为auto,会根据返回的页面编码标志取,如果取不到,可能会报错)
-->
<fetchConfig charset="utf-8" timeOutSecond="5" errorRetry="5" errorDelayTime="10" runThreadNum="5"  fetchPrepareDelayTime="5" >

   <userAgent>Mozilla/5.0 (compatible; webpasser;)</userAgent>
<headers>
<header name="Referer" value="http://www.nhfpc.gov.cn" />
</headers>

<!-- HTTP Cookie -->
<cookies>
<!--   <cookie  name="cookie1" value="" host="" path=""/>
          <cookie name="cookie2" value="1"  /> -->
</cookies>

<!-- 代理设置: 从ip.txt中批量获读取ip,每次抓取随机使用某个ip -->
<!-- <proxies path="ip.txt"></proxies> -->


</fetchConfig>

<!-- 抓取的域名范围 -->
<scope>
<limitHost value="www.nhfpc.gov.cn" />
</scope>

<!-- 种子 -->
<seeds>
<seed  url="http://www.nhfpc.gov.cn/zhuzhan/flfg/lists.shtml" />
</seeds>

<!-- 挖取网页链接,放入抓取队列 -->
<page>
<scope>
  <rule type="regex" value="http://www.nhfpc.gov.cn/zhuzhan/flfg/(.*).shtml" />
</scope>
<!-- 列表数据挖取 -->
    <digLink>
         <rules>
<rule type="xpath" value="//div[@class='contents']//ul//li//a[@href]" attr="href"  />
<rule type="replace" >
    <oldChars>../</oldChars>
       <newChars></newChars>
   </rule>
<rule type="combine" value="http://www.nhfpc.gov.cn/[$this]" />
 </rules>
    </digLink>
    <!-- 分页挖取 -->
 <digLink>
         <rules>
  <rule type="xpath" value="//div[@class='pagination_index_num']//a[@href]" attr="href"  />
<rule type="combine" value="http://www.nhfpc.gov.cn/zhuzhan/flfg/[$this]" />
 </rules>
    </digLink>
</page>

<!-- 解析具体的业务数据,处理后是一个map
 -->
<page name="films">

<scope>
  <rule type="regex" value="http://www.nhfpc.gov.cn/\w+/\w+/\d*/\w+.shtml" />
</scope>


<field name="title" >
<rules>
<!-- 提取某个字段数据的处理链  -->
<rule type="xpath" value="//div[@class='content_title']" exp="text()" />
<rule type="toText" >
</rule>
</rules>
</field>


<field name="subTitle" >
<rules>
<rule type="xpath" value="//div[@class='content_subtitle']//span[1]" exp="text()" />
<rule type="toText" >
</rule>
<!-- 
 <rule type="cut" >
    <pre>年代:</pre>
    <end> </end>
 </rule>
 -->
</rules>
</field>

<field name="submitdate" >
<rules>
<rule type="xpath" value="//div[@class='content_subtitle']//span[3]" exp="text()" />
</rules>
</field>
 
<field name="content" >
<rules>
<rule type="xpath" value="//div[@class='content']" exp="text()" />
<rule type="toText" >
</rule>
</rules>
</field>

<!-- 
<field name="downlist" isList="1" >
        <rules>
<rule type="xpath" value="//div[@class='ndownlist']" exp="html()"   />
 <rule type="cut" >
    <pre>GvodUrls = "</pre>
    <end>";</end>
 </rule>
 <rule type="split" >
  <param name="splitMark" value="###" />
 </rule>
</rules>
</field>
  -->
 
</page>

<!-- 抓取解析后的数据持久化 -->
<resultHandler target="handleResultMapInterface" classPath="com.hxt.webpasser.persistent.impl.DiskJsonHandleResult">
<property name="rootDir" value="d:/www.nhfpc.gov.cn/data" ></property>
<property name="charSet" value="gbk" ></property>
</resultHandler>
 
</task>


@ hxt168

展开
收起
爱吃鱼的程序员 2020-06-09 13:20:39 503 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    这个网站分页是js生成的,要用自定义处理链(项目要再gitpull下)。

    这个分页的处理链简单写了下:

    publicclassCustomDemoRuleimplementsDecideRule{


    publicList<Object>handle(Rulerule,List<Object>contentList,MapvalueMap){
    //valueMap默认会有fetchUrl和 taskName的值
    StringfetchUrl=String.valueOf(valueMap.get("fetchUrl"));
    if(contentList!=null)
    {
    for(inti=0;i<contentList.size();i++)
    {
    //('page_div',4,2,'lists','shtml',80)
    Stringcon=String.valueOf(contentList.get(i));
    String[]arr=con.split(",");
    if(arr.length>5){
    inttotalPage=Integer.parseInt(arr[1]);
    StringlistStr=StringUtil.cutNotContainStartAndEnd(arr[3],"'","'");
    StringshtmlStr=StringUtil.cutNotContainStartAndEnd(arr[4],"'","'");
    StringpreUrl=StringUtil.cutNotContainStartAndEnd(fetchUrl,"",listStr);
    List<Object>list=newArrayList<Object>();
    for(intj=1;j<=totalPage;j++){
    Stringurl=preUrl+listStr+"_"+j+"."+shtmlStr;
    list.add(url);
    }
    returnlist;

    }

    }

    }

    returnnull;
    }



    }


    配置:

    <?xmlversion="1.0"encoding="UTF-8"?>


    <task>


    <!-- timeOutSecond:抓取时请求超时时间(单位:秒)
       errorRetry:抓取失败重连次数
       errorDelayTime:抓取失败后等待时间
     fetchPrepareDelay:抓取前延迟时间(单位:秒),防止对某个网站太频繁爬
     runThreadNum: 线程数
     charset:编码 (如果设为auto,会根据返回的页面编码标志取,如果取不到,可能会报错)
    -->
    <fetchConfigcharset="utf-8"timeOutSecond="5"errorRetry="5"errorDelayTime="10"runThreadNum="5" fetchPrepareDelayTime="5">


      <userAgent>Mozilla/5.0(compatible;webpasser;)</userAgent>
    <headers>
    <headername="Referer"value="http://www.nhfpc.gov.cn"/>
    </headers>


    <!--HTTPCookie-->
    <cookies>
    <!-- <cookie name="cookie1"value=""host=""path=""/>
         <cookiename="cookie2"value="1" />-->
    </cookies>


    <!--代理设置:从ip.txt中批量获读取ip,每次抓取随机使用某个ip-->
    <!--<proxiespath="ip.txt"></proxies>-->




    </fetchConfig>


    <customConfig>
        <!--自定义rule-->
    <customRules>
         <customRulename="digJumpPage"classPath="com.hxt.webpasser.regular.CustomDemoRule"/>
    </customRules>
    </customConfig>


    <!--抓取的域名范围-->
    <scope>
    <limitHostvalue="www.nhfpc.gov.cn"/>
    </scope>


    <!--种子-->
    <seeds>
    <seed url="http://www.nhfpc.gov.cn/zhuzhan/flfg/lists.shtml"/>
    </seeds>


    <!--挖取网页链接,放入抓取队列-->
    <page>
    <scope>
     <ruletype="regex"value="http://www.nhfpc.gov.cn/zhuzhan/(.*)/(.*).shtml"/>
    </scope>
    <!--列表数据挖取-->
      <digLink>
         <rules>
    <ruletype="xpath"value="//div[@class='contents']//ul//li//a[@href]"attr="href" />
    <ruletype="replace">
      <oldChars>../</oldChars>
        <newChars></newChars>
      </rule>
    <ruletype="combine"value="http://www.nhfpc.gov.cn/[$this]"/>
     </rules>
      </digLink>
      <!--分页挖取-->
     <digLink>
         <rules>
       
            <ruletype="cut">
       <!--截取分页js example:createPageHTML('page_div',4,2,'lists','shtml',80);}-->
      <pre><![CDATA[createPageHTML(]]></pre>
      <end><![CDATA[);]]></end>
    </rule>

      <!--自己定义的处理链, 分页-->
    <ruletype="digJumpPage" /> 

     
        </rules>
      </digLink>
    </page>


    <!--解析具体的业务数据,处理后是一个map
     -->
    <pagename="films">


    <scope>
     <ruletype="regex"value="http://www.nhfpc.gov.cn/\w+/\w+/\d*/\w+.shtml"/>
    </scope>




    <fieldname="title">
    <rules>
    <!--提取某个字段数据的处理链 -->
    <ruletype="xpath"value="//div[@class='content_title']"exp="text()"/>
    <ruletype="toText">
    </rule>
    </rules>
    </field>




    <fieldname="subTitle">
    <rules>
    <ruletype="xpath"value="//div[@class='content_subtitle']//span[1]"exp="text()"/>
    <ruletype="toText">
    </rule>
    <!-- 
     <ruletype="cut">
      <pre>年代:</pre>
      <end></end>
     </rule>
     -->
    </rules>
    </field>


    <fieldname="submitdate">
    <rules>
    <ruletype="xpath"value="//div[@class='content_subtitle']//span[3]"exp="text()"/>
    </rules>
    </field>
     
    <fieldname="content"> 
    <rules>
    <ruletype="xpath"value="//div[@class='content']"exp="text()"/>
    <ruletype="toText">
    </rule>
    </rules>
    </field>


    <!-- 
    <fieldname="downlist"isList="1">
        <rules>
    <ruletype="xpath"value="//div[@class='ndownlist']"exp="html()" />
     <ruletype="cut">
      <pre>GvodUrls="</pre>
      <end>";</end>
     </rule>
     <ruletype="split">
     <paramname="splitMark"value="###"/>
     </rule>
    </rules>
    </field>
     -->
      
    </page>


    <!--抓取解析后的数据持久化-->
    <resultHandlertarget="handleResultMapInterface"classPath="com.hxt.webpasser.persistent.impl.DiskJsonHandleResult">
    <propertyname="rootDir"value="d:/www.nhfpc.gov.cn/data"></property>
    <propertyname="charSet"value="gbk"></property>
    </resultHandler>
     
    </task>




    @hxt168谢谢~
    2020-06-09 13:20:55
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
数据采集:日志数据上传 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载