You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.7 KiB

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;
public class HnuNewsStrategy implements CrawlStrategy {
@Override
public boolean supports(String url) {
return url.contains("news.hnu.edu.cn");
}
@Override
public List<Article> parse(String url, Document doc) {
List<Article> articles = new ArrayList<>();
Elements links = doc.select("a[href*='/info/']");
for (Element link : links) {
String articleUrl = link.attr("abs:href");
if (!articleUrl.startsWith("http")) {
articleUrl = "https://news.hnu.edu.cn" + link.attr("href").replace("..", "");
}
String title = link.text().trim();
if (title.isEmpty()) {
Element titleEl = link.selectFirst("h2, h3, h4, .title");
if (titleEl != null) {
title = titleEl.text().trim();
}
}
String content = "";
Element parent = link.parent();
if (parent != null) {
Element contentEl = parent.selectFirst("p, .summary, .description");
if (contentEl != null) {
content = contentEl.text().trim();
}
}
if (!title.isEmpty() && articleUrl.contains("/info/")) {
articles.add(new Article(title, articleUrl, content));
}
}
return articles;
}
}