因為項目需求,需要車輛品牌信息和車系信息,昨天用一天時間研究了jsoup爬取網站信息。項目是用maven+spring+springmvc+mybatis寫的。
這個是需要爬取網站的地址 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.運行結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/fengzhifei/archive/2018/01/30/8383448.html