当前位置:Gxlcms > JavaScript > jsoup怎么把爬取网站的图片保存到本地

jsoup怎么把爬取网站的图片保存到本地

时间:2021-07-01 10:21:17 帮助过:18人阅读

这次给大家带来jsoup怎么把爬取网站的图片保存到本地,jsoup把爬取网站的图片保存到本地的注意事项有哪些,下面就是实战案例,一起来看一下。

因为项目需求,需要车辆品牌信息和车系信息,昨天用一天时间研究了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("************************"); } } } }

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

JS提示文本框邮箱地址补全

getBoundingClientRect使用方法及兼容性处理

以上就是jsoup怎么把爬取网站的图片保存到本地的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行