jsoupはどうやって写真を地元に取りに行きますか?


プロジェクトの需要のため、車のブランド情報と車の情報が必要です。昨日は一日でjsoupを研究しました。プログラムはmaven+spring+springmvc+mybatisで書きました。
jsoup開発ガイドの住所 
これはウェブサイトを登らなければならない住所です。https://car.autohome.com.cn/zhaoche/pinpai/
1.まずpom.xmlに依存を追加する
写真を地元に保存する必要がありますので、またcomons-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();
      //           
      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);
        }
        /**
         *               
         *                  
         *                  
         *   
         */
        
        //      
        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){
          //        
          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("      -----------" + hezipinpai);
            }else{
              //int childNodeSize_5 = element.child(i).child(1).child(0).childNodeSize();
              //            
              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.運転結果

以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。