Home > Web Front-end > JS Tutorial > body text

How does jsoup save images from crawled websites locally?

php中世界最好的语言
Release: 2018-04-13 09:50:51
Original
2360 people have browsed it

This time I will show you how jsoup saves the pictures of the crawled website to the local. What are the things to note? The following is a practical case. , let’s take a look. Because

project requirements

require vehicle brand information and car series information, I spent a day yesterday studying jsoup crawling website information. The project is written using maven spring springmvc mybatis. jsoup development guide address

This is the address of the website that needs to be crawled

https://car.autohome.com.cn/zhaoche/pinpai/ 1. First add dependencies

in pom.xml Because I need to save the image locally, I added the commons-net package

<!-- 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>
Copy after login

2. Implementation of crawler code

@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("************************");
        
      }
    }
  }
}
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

JS prompt text box email address completion


getBoundingClientRect usage and compatibility handling


The above is the detailed content of How does jsoup save images from crawled websites locally?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template