分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

jsoup爬取图片到本地

发布时间:2023-09-06 01:40责任编辑:林大明关键词:js

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

  jsoup开发指南地址:http://www.open-open.com/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.运行结果

  4.

jsoup爬取图片到本地

原文地址:https://www.cnblogs.com/fengzhifei/p/8383448.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved