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.
32 lines
944 B
32 lines
944 B
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 NewsStrategy implements CrawlStrategy {
|
|
// 使用正则匹配
|
|
private static final Pattern URL_PATTERN = Pattern.compile(".*news\\.example\\.com.*");
|
|
@Override
|
|
public boolean supports(String url) {
|
|
return URL_PATTERN.matcher(url).matches();
|
|
}
|
|
|
|
@Override
|
|
public List<Article> parse(String url, Document doc) {
|
|
List<Article> articles = new ArrayList<>();
|
|
Elements items = doc.select(".article-headline");
|
|
for (Element e : items) {
|
|
articles.add(new Article(e.text(), url, ""));
|
|
}
|
|
return articles;
|
|
}
|
|
@Override
|
|
public int getPriority(){
|
|
return 10;
|
|
}
|
|
}
|
|
|