Browse Source

删除 'project/java-cli/src/main/java/com/example/datacollect/strategy/CCTVStrategy.java'

main
LeiJuntao 3 weeks ago
parent
commit
92307697fd
  1. 49
      project/java-cli/src/main/java/com/example/datacollect/strategy/CCTVStrategy.java

49
project/java-cli/src/main/java/com/example/datacollect/strategy/CCTVStrategy.java

@ -1,49 +0,0 @@
package com.example.datacollect.strategy;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.example.datacollect.exception.ParseException;
import com.example.datacollect.model.Article;
public class CCTVStrategy implements CrawlStrategy {
@Override
public boolean supports(String url) {
return url.contains("cctv.com");
}
@Override
public List<Article> parse(String url, Document doc) throws ParseException {
List<Article> articles = new ArrayList<>();
Elements listItems = doc.select("ul:not([class]) li");
for (Element li : listItems) {
Element link = li.selectFirst("a");
if (link == null) continue;
String articleUrl = link.attr("href");
if (!articleUrl.startsWith("http")) {
if (articleUrl.startsWith("//")) {
articleUrl = "https:" + articleUrl;
} else if (articleUrl.startsWith("/")) {
articleUrl = "https://www.cctv.com" + articleUrl;
} else {
articleUrl = "https://www.cctv.com/" + articleUrl;
}
}
String title = link.text().trim();
String content = "";
if (!title.isEmpty() && title.length() > 10) {
articles.add(new Article(title, articleUrl, content));
}
}
return articles;
}
}
Loading…
Cancel
Save