日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

爬虫 spider06——解析数据

發布時間:2024/2/28 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬虫 spider06——解析数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

解析數據本質上就是在解析HTML文檔,

  • 如何解析HTML文檔呢?
  • js 和 jQuery 就可以解析HTML文檔, 但是js 和jQuery是前端的技術, 而目前HTML代碼存儲在java代碼中, 故無法使用js和jQuery來解析HTML文檔
  • 那么需要使用一種可以在java客戶端來解析HTML文檔的技術 --->jsoup

官網:https://jsoup.org?

?jsoup是一款java解析HTML文檔的工具, 如果要使用jsoup 需要先進行導包

? ? ? ??

<dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.10.3</version></dependency>
  • jsoup在使用之前必須先獲取到document對象

獲取document對象?

01:

@Test //根據標簽名稱public void jsoupParse01(){String html=" <html>\n" +" <head>\n" +" <title>First parse</title>\n" +" </head>\n" +" <body>\n" +" <p>Parsed HTML into a doc 01.</p>\n" +" <p>Parsed HTML into a doc 02.</p>\n" +" </body>\n" +" </html>";Document document = Jsoup.parse(html);System.out.println("title:"+document.title());Elements ps = document.getElementsByTag("p");System.out.println(ps.size());Element p1 = ps.get(0);System.out.println(p1.text());Element p2 = ps.get(1);System.out.println(p2.text());}

結果顯示:

?


?

02:

@Test //根據選擇器public void jsoupParse02(){String html=" <html>\n" +" <head>\n" +" <title>First parse</title>\n" +" </head>\n" +" <body>\n" +" <p class='clsPHTML'>Parsed HTML into a doc 01.</p>\n" +" <p id='doc02'>Parsed HTML into a doc 02.</p>\n" +" </body>\n" +" </html>";Document document = Jsoup.parse(html);//選擇器:根據id查找節點Elements p = document.select("p#doc02");System.out.println(p.text());//選擇器:根據class查找節點Elements pcls = document.select("p.clsPHTML");System.out.println(pcls.text());//選擇器:根據節點層次查找節點Elements bodyp = document.select("body > p");System.out.println(bodyp.text());}

?結果顯示:


?

03:

@Test //根據url解析頁面public void jsoupParse03() throws IOException {String html="http://www.manmanbuy.com/";Document document = Jsoup.connect(html).get();System.out.println(document.title());}

?結果顯示:


04:

@Test //根據本地文件解析頁面public void jsoupParse04() throws IOException {String html="d:\\a.html";Document document = Jsoup.parse(new File(html),"UTF-8");//選擇器:根據id查找節點Elements p = document.select("p#doc02");System.out.println(p.text());}

?結果顯示:


05:

@Test //爬取慢慢買商品分類public void jsoupParse06() throws IOException {String html="http://www.manmanbuy.com/";Document document = Jsoup.connect(html).get();Elements goods = document.select("div[class=sub-nav] > ul > li > div[class=hd] > a");for (Element good : goods) {System.out.println(good.text());}}

?結果展示:


總結

總結: jsoup常用方法

  • parse(String html) ; 獲取document對象
  • select("選擇器"); 根據指定的選擇器獲取元素
  • text()/html(); 獲取內容體的數據, text方法獲取文本內容 html方法主要使用用來獲取文本+html內容
  • <a>跳轉首頁</a>
  • attr(String key); 根據指定屬性名稱獲取屬性值

?

保存數據

目前將數據保存到MySQL 或者 文件中 , 后期數據保存到hadoop hbase

  • 如何保存到MySQL中:
  • JDBC
  • mybatis
  • spring中JDBCTemplate

?

?

?

?

總結

以上是生活随笔為你收集整理的爬虫 spider06——解析数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。