因為項目需求,需要車輛品牌信息和車系信息,昨天用一天時間研究了jsoup爬取網站信息。項目是用maven+spring+springmvc+mybatis寫的。
jsoup開髮指南地址
這個是需要爬取網站的地址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(); //循環獲取子標籤內的內容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.運行結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。