Webcollector 爬取淘宝页面小程序

package com.si.demo;

import org.jsoup.nodes.Document;

import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler;


public class MyCrawler extends BreadthCrawler {
    
    public MyCrawler(String crawlPath, boolean autoParse) {
         super(crawlPath, autoParse);
         //添加初始页面
         this.addSeed("https://www.taobao.com/");
         
         /*获取像这样的http://news.hfut.edu.cn/show-xxxxxxhtmlurl;设置页面过滤规则,‘+’是可通过的,‘-’是不可通过的*/
         this.addRegex("+https://www.taobao.com/market/nvxie/.*");
         /*不获取这样的格式 jpg|png|gif*/
         this.addRegex("-.*\\.(jpg|png|gif).*");
         /*也不要这样的 #*/
         this.addRegex("-.*#.*");

    }


    //自己设置获取信息
    public void visit(Page page, CrawlDatums next) {
        String url = page.url();
        /*如果是一个新的页面*/
        if (page.matchUrl("https://www.taobao.com/market/nvxie/.*")) {
            /*用JSOUP去解析这个页面*/
            Document doc = page.doc();
            /*的提取新闻和标题的css选择器*/
            String title = page.select("head title").first().text();
            String content = page.select("ul li").text();
     
            System.out.println("URL:\n" + url);
            System.out.println("title:\n" + title);
            System.out.println("content:\n" + content);
     
            /*如果你想要抓取新的url,添加他们到 nextLink*/
            /*WebCollector 可以自动去重*/
            /*如果autoparse 为真则添加链接去nextlinks 如果与正则不匹配则不会添加*/
            //next.add("http://xxxxxx.com");
        }

    }

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        MyCrawler crawler = new MyCrawler("crawl", true);
        crawler.setThreads(5);
        
        crawler.setResumable(true);
        /*网页爬取深度为4*/
        crawler.start(4);
    }
  


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值