From 621e8b9d4d3e6ef56c5167831880bcb62eafbb14 Mon Sep 17 00:00:00 2001 From: zhangsiyuan <3837703520@qq.com> Date: Thu, 7 May 2026 17:38:18 +0800 Subject: [PATCH] =?UTF-8?q?w10-=E5=BC=A0=E6=80=9D=E6=B8=8A-202401070104?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w10/BlogStrategy.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 w10/BlogStrategy.java diff --git a/w10/BlogStrategy.java b/w10/BlogStrategy.java new file mode 100644 index 0000000..6ed4c58 --- /dev/null +++ b/w10/BlogStrategy.java @@ -0,0 +1,33 @@ +package com.example.datacollect.strategy; + +import com.example.datacollect.model.Article; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +public class BlogStrategy implements CrawlStrategy { + private static final Pattern URL_PATTERN = Pattern.compile("https?://[^/]*blog\\.example\\.com.*"); + + @Override + public boolean supports(String url) { + return url != null && URL_PATTERN.matcher(url).matches(); + } + + @Override + public int getPriority() { + return 50; + } + + @Override + public List
parse(String url, Document doc) { + List
articles = new ArrayList<>(); + Elements titles = doc.select(".post-title"); + for (Element e : titles) { + articles.add(new Article(e.text(), url, "")); + } + return articles; + } +}