1 changed files with 0 additions and 49 deletions
@ -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 GmwStrategy implements CrawlStrategy { |
|||
@Override |
|||
public boolean supports(String url) { |
|||
return url.contains("gmw.cn"); |
|||
} |
|||
|
|||
@Override |
|||
public List<Article> parse(String url, Document doc) throws ParseException { |
|||
List<Article> articles = new ArrayList<>(); |
|||
Elements listItems = doc.select("ul.m_ulList 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.gmw.cn" + articleUrl; |
|||
} else { |
|||
articleUrl = "https://www.gmw.cn/" + articleUrl; |
|||
} |
|||
} |
|||
|
|||
String title = link.text().trim(); |
|||
String content = ""; |
|||
|
|||
if (!title.isEmpty() && title.length() > 10) { |
|||
articles.add(new Article(title, articleUrl, content)); |
|||
} |
|||
} |
|||
|
|||
return articles; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue