溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

jsoup如何爬取圖片到本地

發(fā)布時間:2020-10-21 17:24:15 來源:腳本之家 閱讀:127 作者:殤丨恨 欄目:編程語言

因為項目需求,需要車輛品牌信息和車系信息,昨天用一天時間研究了jsoup爬取網站信息。項目是用maven+spring+springmvc+mybatis寫的。

jsoup開發(fā)指南地址 

這個是需要爬取網站的地址 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.爬蟲代碼的實現

@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 {
    //定義想要爬取數據的地址
    String url = "https://car.autohome.com.cn/zhaoche/pinpai/";
    //獲取網頁文本
    Document doc = Jsoup.connect(url).get();
    //根據類名獲取文本內容
    Elements elementsByClass = doc.getElementsByClass("uibox-con");
    //遍歷類的集合
    for (Element element : elementsByClass) {
      //獲取類的子標簽數量
      int childNodeSize_1 = element.childNodeSize();
      //循環(huán)獲取子標簽內的內容
      for (int i = 0; i < childNodeSize_1; i++) {
        //獲取車標圖片地址
        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();
        //輸出獲取內容看是否正確
        System.out.println("車標圖片地址-----------" + tupian);
        System.out.println("品牌-----------" + pinpai);
        System.out.println();
        //把車標圖片保存到本地
        String tupian_1 = "http:"+tupian;
        //連接url
        URL url1 = new URL(tupian_1);
        URLConnection uri=url1.openConnection();
        //獲取數據流
        InputStream is=uri.getInputStream();
        //獲取后綴名
        String imageName = tupian.substring(tupian.lastIndexOf("/") + 1,tupian.length());
        //寫入數據流
        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);
        }
        /**
         * 因為每個品牌下有多個合資工廠
         * 比如一汽大眾和上海大眾還有進口大眾
         * 所有需要循環(huán)獲取合資工廠名稱和旗下
         * 車系
         */
        
        //獲取車系數量
        int childNodeSize_2 = element.child(i).child(1).child(0).childNodeSize();
        /**
         * 獲取標簽下子標簽數量
         * 如果等于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是單數則是合資工廠名稱
             * 否則是車系信息
             */
            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.運行結果

jsoup如何爬取圖片到本地jsoup如何爬取圖片到本地jsoup如何爬取圖片到本地

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI