项目托管平台: 码云地址:https://gitee.com/HDMBS/JavaSpiderDemo.git
本DemoMaven_Jar
<!-- HttpClient支持 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<!--Jsoup支持 -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
public static void main(String[] args) throws IOException {
// 用httpClient获取网页(如果传入代理IP则用代理IP,不传则用当前IP)
SpiderFruit pageContent = downloadss.getPageContent("https://www.cnblogs.com/");
// 用Jsoup将网页解析为文档对象
Document doc = Jsoup.parse(pageContent.getEntitystr());
System.out.println("--------------------");
//1.获取任意标签返回多个元素
/*
* [获取HTML标签是title的所有元素]Demo1 获取文章
* https://zhuanlan.zhihu.com/p/30974799
* Elements h2s = doc.getElementsByTag("h2");
* // 根据HTML_Tag名称来获得多个标签字段;
* Elements blockquotes =doc.getElementsByTag("blockquote");
* for (int i = 0; i < h2s.size(); i++) {
* //获取索引上的元素并以文本展示
* System.out.println(h2s.get(i).text());
* System.out.println(blockquotes.get(i).text());
* System.out.println();
*
* }
*
*/
//2.使用标签ID获取元素获得单个元素
/*
*
String elementById = doc.getElementById("shicineirong").text();
System.out.println(elementById);
*/
//3.使用标签Class获取元素
/*
* //如果是用文本显示:则标签获取匹配的第一个Class所有元素并以文本展示
//如果是用Html显示:则符合的标签全部显示
String text = doc.getElementsByClass("post_item").text();
System.out.println(text);
*/
}