jsoup如何爬取圖片到本地
因為項目需求,需要車輛品牌信息和車系信息,昨天用一天時間研究了jsoup爬取網(wǎng)站信息。項目是用maven+spring+springmvc+mybatis寫的。
這個是需要爬取網(wǎng)站的地址 https://car.autohome.com.cn/zhaoche/pinpai/
1.首先在pom.xml中添加依賴
因為需要把圖片保存到本地所以又添加了commons-net包
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.10.3</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.3</version> </dependency>
2.爬蟲代碼的實現(xiàn)
@Controller @RequestMapping("/car/") public class CarController { //圖片保存路徑 private static final String saveImgPath="C://imgs"; /** * @Title: insert 品牌名稱 和圖片爬取和添加 * @Description: * @param @throws IOException * @return void * @throws * @date 2018年1月29日 下午4:42:57 */ @RequestMapping("add") public void insert() throws IOException { //定義想要爬取數(shù)據(jù)的地址 String url = "https://car.autohome.com.cn/zhaoche/pinpai/"; //獲取網(wǎng)頁文本 Document doc = Jsoup.connect(url).get(); //根據(jù)類名獲取文本內(nèi)容 Elements elementsByClass = doc.getElementsByClass("uibox-con"); //遍歷類的集合 for (Element element : elementsByClass) { //獲取類的子標(biāo)簽數(shù)量 int childNodeSize_1 = element.childNodeSize(); //循環(huán)獲取子標(biāo)簽內(nèi)的內(nèi)容 for (int i = 0; i < childNodeSize_1; i++) { //獲取車標(biāo)圖片地址 String tupian = element.child(i).child(0).child(0).child(0).child(0).attr("src"); //獲取品牌名稱 String pinpai = element.child(i).child(0).child(1).text(); //輸出獲取內(nèi)容看是否正確 System.out.println("車標(biāo)圖片地址-----------" + tupian); System.out.println("品牌-----------" + pinpai); System.out.println(); //把車標(biāo)圖片保存到本地 String tupian_1 = "http:"+tupian; //連接url URL url1 = new URL(tupian_1); URLConnection uri=url1.openConnection(); //獲取數(shù)據(jù)流 InputStream is=uri.getInputStream(); //獲取后綴名 String imageName = tupian.substring(tupian.lastIndexOf("/") + 1,tupian.length()); //寫入數(shù)據(jù)流 OutputStream os = new FileOutputStream(new File(saveImgPath, imageName)); byte[] buf = new byte[1024]; int p=0; while((p=is.read(buf))!=-1){ os.write(buf, 0, p); } /** * 因為每個品牌下有多個合資工廠 * 比如一汽大眾和上海大眾還有進(jìn)口大眾 * 所有需要循環(huán)獲取合資工廠名稱和旗下 * 車系 */ //獲取車系數(shù)量 int childNodeSize_2 = element.child(i).child(1).child(0).childNodeSize(); /** * 獲取標(biāo)簽下子標(biāo)簽數(shù)量 * 如果等于1則沒有其他合資工廠 */ int childNodeSize_3 = element.child(i).child(1).childNodeSize(); if(childNodeSize_3==1){ //循環(huán)獲取車系信息 for (int j = 0; j < childNodeSize_2; j++) { String chexi = element.child(i).child(1).child(0).child(j).child(0).child(0).text(); System.out.println("車系-----------" + chexi); } }else{ /** * 如果childNodeSize_3大于1 * 則有多個合資工廠 */ //分別獲取各個合資工廠旗下車系 for (int j = 0; j < childNodeSize_3; j++) { int childNodeSize_4 = element.child(i).child(1).child(j).childNodeSize(); /** * 如果j是單數(shù)則是合資工廠名稱 * 否則是車系信息 */ int k = j%2; if(k==0){ //獲取合資工廠信息 String hezipinpai = element.child(i).child(1).child(j).child(0).text(); System.out.println("合資企業(yè)名稱-----------" + hezipinpai); }else{ //int childNodeSize_5 = element.child(i).child(1).child(0).childNodeSize(); //循環(huán)獲取合資工廠車系信息 for(int l = 0; l < childNodeSize_4; l++){ String chexi = element.child(i).child(1).child(j).child(l).child(0).child(0).text(); System.out.println("車系-----------" + chexi); } } } } System.out.println("************************"); System.out.println("************************"); } } } }
3.運行結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java語言中finally是否一定會執(zhí)行你知道嗎
這篇文章主要為大家詳細(xì)介紹了Java finally是否一定會執(zhí)行,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02Java SpringMVC 集成靜態(tài)資源的方式你了解嗎
本篇文章主要介紹了SpringMVC集成靜態(tài)資源的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-10-10Java數(shù)據(jù)結(jié)構(gòu)超詳細(xì)分析二叉搜索樹
二叉搜索樹是以一棵二叉樹來組織的。每個節(jié)點是一個對象,包含的屬性有l(wèi)eft,right,p和key,其中,left指向該節(jié)點的左孩子,right指向該節(jié)點的右孩子,p指向該節(jié)點的父節(jié)點,key是它的值2022-03-03