引入依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
创建Java类
private static final String URL = "https://blog.csdn.net/weixin_44668443?type=blog";
public static void main(String[] args){
try {
Document document = Jsoup.connect(URL).get();
Elements articleElements = document.select("article.blog-list-box[data-v-6fe2b6a7]");
articleElements.forEach(articleElement -> {
Element h4Element = articleElement.selectFirst("div.blog-list-box-top h4[data-v-6fe2b6a7]");
Element linkElement = articleElement.selectFirst("a[data-v-6fe2b6a7]");
if (h4Element != null && linkElement != null) {
System.out.println("title: " + h4Element.text());
System.out.println("url: " + linkElement.attr("href"));
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
结果