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.

45 lines
1.2 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;
import java.util.regex.Pattern;
public class RegexStrategy implements CrawlStrategy {
private final Pattern pattern;
private final int priority;
public RegexStrategy(String regex, int priority) {
this.pattern = Pattern.compile(regex);
this.priority = priority;
}
@Override
public boolean supports(String url) {
return pattern.matcher(url).matches();
}
@Override
public int getPriority() {
return priority;
}
@Override
public List<Article> parse(String url, Document doc) {
List<Article> articles = new ArrayList<>();
Elements items = doc.select("a[href]");
for (Element item : items) {
String text = item.text();
if (!text.isEmpty()) {
String href = item.attr("href");
String fullUrl = href.startsWith("http") ? href : url + href;
articles.add(new Article(text, fullUrl, ""));
}
}
return articles;
}
}