当前位置: 首页 > backend >正文

Spring Boot 使用 WebMagic 爬虫框架入门

1. 创建 Spring Boot 项目

使用 Spring Initializr 创建一个 Spring Boot 项目,选择需要的依赖,如 Spring Web 等。

2. 添加 WebMagic 依赖

在项目的 pom.xml 文件中添加 WebMagic 的核心和扩展依赖:

<dependency><groupId>us.codecraft</groupId><artifactId>webmagic-core</artifactId><version>0.10.0</version>
</dependency>
<dependency><groupId>us.codecraft</groupId><artifactId>webmagic-extension</artifactId><version>0.10.0</version>
</dependency>

3. 编写爬虫处理器

创建一个类实现 PageProcessor 接口,重写 process 方法和 getSite 方法。

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;public class MyPageProcessor implements PageProcessor {// 抓取网站的相关配置private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);@Overridepublic void process(Page page) {// 定义如何抽取页面信息,并保存下来page.putField("title", page.getHtml().xpath("//title/text()").toString());page.putField("content", page.getHtml().css("div.content").toString());// 从页面发现后续的 url 地址来抓取page.addTargetRequests(page.getHtml().css("a", "href").all());}@Overridepublic Site getSite() {return site;}public static void main(String[] args) {Spider.create(new MyPageProcessor()).run();}
}

4. 配置 Pipeline

Pipeline 用于处理爬取到的数据。可以创建一个自定义的 Pipeline,将数据存储到数据库或其他位置。

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;public class MyPipeline implements Pipeline {@Overridepublic void process(ResultItems resultItems, Task task) {// 获取爬取的数据String title = resultItems.get("title");String content = resultItems.get("content");// 将数据存储到数据库或其他操作System.out.println("标题:" + title + ", 内容:" + content);}
}

5. 启动爬虫

在 Spring Boot 的启动类中,调用爬虫的启动方法。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import us.codecraft.webmagic.Spider;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);// 启动爬虫Spider.create(new MyPageProcessor()).addPipeline(new MyPipeline()).run();}
}

6. 注意事项

  • 网站反爬虫机制:在爬取网站时,要注意遵守网站的规则和反爬虫机制,合理设置爬取的间隔时间和重试次数,避免对网站造成过大压力。

  • 数据存储:根据实际需求选择合适的数据存储方式,如数据库、文件等,并在 Pipeline 中实现相应的存储逻辑。

  • 异常处理:在爬虫过程中可能会出现网络异常、解析异常等情况,需要添加适当的异常处理逻辑,保证爬虫的稳定运行。

http://www.xdnf.cn/news/3409.html

相关文章:

  • 腾讯云BI VS quickbi 企业选型(从企业实际功能使用和费用对比)
  • 在项目中如何对Map List等对象序列化及反序列化
  • 雅马哈SMT贴片机高效精密制造解析
  • 【数据结构】线性表--顺序表
  • 59常用控件_QComboBox的使用
  • 【C语言练习】015. 声明和初始化指针
  • 【Hive入门】Hive性能调优之资源配置:深入解析执行引擎参数调优
  • 欧拉计划 Project Euler62(立方数重排)题解
  • Allegro23.1新功能之如何加粗打印线宽操作指导
  • 跨域 iframe 内剪切板 Clipboard_API 报错
  • 网络安全零基础培训 L1-9 PHP连接MySQL数据库
  • d202551
  • QMK固件烧录指南:安全高效地更新您的机械键盘
  • Python结合QT进行开发
  • 西门子数字化研发设计制造一体化规划案例P87(87页PPT)(文末有下载方式)
  • 神经网络—损失函数
  • Python 数据智能实战 (6):用户评论深度挖掘
  • OpenGL-ES 学习(10) ---- OpenGL-ES Shader语言语法
  • CMake中强制启用option定义变量的方法
  • Unity SpriteEditor(精灵图片编辑器)
  • C++笔记-继承(下)(包含派生类的默认成员函数,菱形继承等)
  • AJAX 实例
  • vscode 的空格和 tab 设置 与 Rime 自建词库
  • AI大模型基础设施:主流的几款开源AI大语言模型的本地部署成本
  • 企业内训|智能驾驶与智能座舱技术——某汽车厂商
  • Ubuntu18 登录界面死循环 Ubuntu进不了桌面
  • 初学Vue之记事本案例
  • 【Linux】VSCode用法
  • 【嵌入式———通用定时器基本操作——实验需求2:案列:测量PWM的频率/周期】
  • 用手机相册教我数组概念——照片分类术[特殊字符][特殊字符]