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.

40 lines
1.1 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 DefaultStrategy implements CrawlStrategy {
@Override
public boolean supports(String url) {
return true;
}
@Override
public int getPriority() {
return Integer.MIN_VALUE;
}
@Override
public List<Article> parse(String url, Document doc) {
List<Article> articles = new ArrayList<>();
Elements headings = doc.select("h1, h2, h3, h4, h5, h6");
for (Element h : headings) {
String text = h.text().trim();
if (!text.isEmpty()) {
articles.add(new Article(text, url, ""));
}
}
if (articles.isEmpty()) {
String title = doc.title();
if (!title.isEmpty()) {
articles.add(new Article(title, url, ""));
}
}
return articles;
}
}