服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - jsoup如何爬取图片到本地

jsoup如何爬取图片到本地

2021-03-29 10:06殇丨恨 JAVA教程

这篇文章主要为大家详细介绍了jsoup如何爬取图片到本地,jsoup爬取网站信息,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

因为项目需求,需要车辆品牌信息和车系信息,昨天用一天时间研究了jsoup爬取网站信息。项目是用maven+spring+springmvc+mybatis写的。

jsoup开发指南地址

这个是需要爬取网站的地址 https://car.autohome.com.cn/zhaoche/pinpai/

1.首先在pom.xml中添加依赖

因为需要把图片保存到本地所以又添加了commons-net包

?
1
2
3
4
5
6
7
8
9
10
11
12
<!-- 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.爬虫代码的实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
@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.运行结果

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/fengzhifei/archive/2018/01/30/8383448.html

延伸 · 阅读

精彩推荐