Python之webmagic爬虫优点与使用

WebMagic是一个Java实现的爬虫框架,它具有模块化设计,易于扩展,支持CSS选择器、XPath和正则表达式进行数据抽取,提供下载器、页面处理器和管道组件。尽管不支持JavaScript渲染,但其多线程和分布式特性使其适合中大型网站抓取。通过Pipeline可实现数据持久化,如保存到文件或数据库。
摘要由CSDN通过智能技术生成

一、webmagic的优点

  1. 它更偏向于java的语法,对于熟悉java的工程师来说学习成本较低。

  1. 提供多种选择器,如css选择器、xpath、正则等。

  1. 有一个模块pipeline:可通过简单地配置,可以将爬虫抽取到的信息,持久化到文件、数据库等。

4.模块化的结构,可轻松扩展。

5.提供多线程和分布式支持。

(缺点:不支持JS页面抓取)

二、webmagic的构成

Downloader:负责请求url获取访问的数据(html页面、json等)。

PageProcessor:解析Downloader获取的数据。

Pipeline:PageProcessor解析出的数据由Pipeline来进行保存或者说叫持久化。

Scheduler:调度器通常负责url去重,或者保存url队列,PageProcessor解析出的url可以加入Scheduler队列,用于下一次的爬取。

三、webmagic的核心方法

addUrl:

    public Spider addurl(String... urls) {
        for (String url : urls) {
            addRequest(new Request(url));
        }
        signalNewUr1();
        return this;
    }

scheduler.push(request, this):把需要爬取的url加入到Scheduler队列。

    private void addRequest(Request request) {
        if (site.getDomain() == null && request != null && request.getUrl() != null) {
            site.setDomain(UrlUtils.getDomain(request.getUrl()));
        }
        Jscheduler.push(request, this); 
    }

initComponent:初始化downloader、pipelines、threadPool线程池,webmagic默认down是HttpClientDownloader、默认pipeline是ConsolePipeline

protected void initComponent() {
    if(downloader == null){
        this.downloader = new HttpClientDownloader();
    }
    if (pipelines.isEmpty()) {
        pipelines.add(new ConsolePipeline());
    }
    downloader.setThread(threadNum);
    if(threadPool==null || threadPool.isShutdown()){
        if (executorService != null && !executorService.isShutdown()){
            threadPool = new CountableThreadPool(threadNum, executorService);
        } else {
        threadPool = new CountableThreadPool(threadNum);
        }
    }
    if (startRequests != null){
            for(Request request:startRequests){
                addRequest(request);
            }
            startRequests.clear();
        }
        startTime=new Date();
    }

四、webmagic的使用

导入依赖:

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
<!--            <scope>test</scope>-->
        </dependency>

        <!--webmagic-->
        <dependency>
            <groupId>us.codecraft</groupId>
            <artifactId>webmagic-core</artifactId>
            <version>0.7.4</version>
        </dependency>
        <dependency>
            <groupId>us.codecraft</groupId>
            <artifactId>webmagic-extension</artifactId>
            <version>0.7.4</version>
        </dependency>
实现PageProcessor
抽取元素Selectable

WebMagic里主要使用了三种抽取技术:XPath、正则表达式和CSS选择器。另外,对于JSON格式的内容,可使用JsonPath进行解析。.

1. XPath:语法教程
// Xpath解析
page.putField("div2",page.getHtml().xpath("//div[@id=shortcut-2014]/div/ul/li/a"));
2.使用CSS选择器(主要使用方式):
// 解析返回的数据page,并且把解析的结果放到ResultItems中
page.putField("div",page.getHtml().css("ul.fr li a.link-login").all());
3.使用正则表达式(难度较大):
// 使用正则表达式
page.putField("div3",page.getHtml().css("div#shortcut-2014 a").regex(".*我的.*").all());
抽取部分API:
获得结果API:
测试:
// 处理结果的api
        page.putField("div3",page.getHtml().css("div#shortcut-2014 a").regex(".*我的.*").all());
        page.putField("div4",page.getHtml().css("div#shortcut-2014 a").regex(".*我的.*").get());
        page.putField("div5",page.getHtml().css("div#shortcut-2014 a").regex(".*我的.*").toString());
        page.putField("div6",page.getHtml().css("div#shortcut-2014 a").regex(".*我的.*").match());

使用Pipeline

1输出到控制台或文件
public static void main(String[] args) {
    Spider.create(new GithubRepoPageProcessor())
            //从"https://github.com/code4craft"开始抓
            .addUrl("https://github.com/code4craft")
            //输出到文件
            .addPipeline(new FilePipeline("D:\\webmagic\\"))
            //开启5个线程抓取
            .thread(5)
            //启动爬虫
            .run();
}
2.输出到数据库
2.1定义一个类,实现Pipeline接口
@Component
public class MybatisPipeline implements Pipeline {

    @Autowired
    private JobInfoService jobInfoService;


    @Override
    public void process(ResultItems resultItems, Task task) {
        // 获取封装好的招聘详情对象
        JobInfo jobInfo = resultItems.get("jobInfo");
        // 判断数据是否不为空
        if (jobInfo != null){
            // 如果不为空,则将其保存到数据库当中
            jobInfoService.save(jobInfo);
        }
    }
}
2.2在PageProcessor中 引入定制的 Pipeline,并且在Spider添加进去
//将自定义的 PipeLine注入到Process中
    @Autowired
    private MybatisPipeline mybatisPipeline;


    // 开启定时任务(initialDelay: 初始化的任务开启时间(项目启动多久后开启这个任务),fixedDelay:间隔多久再次开启)
    @Scheduled(initialDelay = 1000, fixedDelay = 100 * 1000) // 单位毫秒 1000毫秒 = 1秒
    public void process() {
        Spider.create(new JobProcessor())
                .addUrl(url)
                // 设置Scheduler和布隆过滤器
                .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(100000)))
                .thread(10) //10个线程进行爬取
                // 添加Pipeline
                .addPipeline(mybatisPipeline)
                .run();
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kblzxj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值