批量下载百度图片

pclin
26
2024-01-08

引入依赖

		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>4.5.15</version>
		</dependency>
		<dependency>
			<groupId>com.squareup.okhttp3</groupId>
			<artifactId>okhttp</artifactId>
			<version>3.10.0</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.22</version>
			<optional>true</optional>
		</dependency>
 		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>

代码实现

  public static void main(String[] args)
    {
		//设置路径 D:/Images/明星/ 设置关键词 Arrays.asList("张天爱","迪丽热巴")
        downloadImg("D:/Images/明星/", Arrays.asList("张天爱","迪丽热巴"));
    }
    public static int downloadImg(String keyword, int currentPage,String path) throws InterruptedException
    {
        HashMap<String, String> header = new HashMap<>();
        header.put("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        header.put("Connection", "keep-alive");
        header.put("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0");
        header.put("Upgrade-Insecure-Requests", "1");
        HashMap<String, Object> params = new HashMap<>();
        params.put("charset", "UTF-8");
        params.put("tn", "resultjson_com");
        params.put("ipn", "rj");
        params.put("ct", "201326592");
        params.put("fp", "result");
        params.put("cl", "2");
        params.put("lm", "-1");//动图
        params.put("ie", "utf-8");
        params.put("oe", "utf-8");
        params.put("st", "-1");
        params.put("ic", "0");
        params.put("istype", "2");
        params.put("qc", "");
        params.put("nc", "1");
        params.put("pn", "0" + currentPage * 50);
        params.put("rn", "50");//每页数量
        params.put("word", keyword);


        OkHttpClient client = new OkHttpClient();
        Headers headers = Headers.of(header);
        StringBuilder urlBuilder = new StringBuilder("https://image.baidu.com/search/acjson?");
        params.forEach((k, v) -> {
            urlBuilder.append(k);
            urlBuilder.append("=");
            urlBuilder.append(v);
            urlBuilder.append("&");
        });
        Request request = new Request.Builder().headers(headers).url(urlBuilder.toString()).get()
                                               .build();
        try (Response response = client.newCall(request).execute();)
        {
            if (response.body() != null)
            {
                String body = response.body().string();
                JSONObject jsonObject = JSON.parseObject(body);
                List<SearchUtil.ImgUrl> imgUrlList = jsonObject.getJSONArray("data").toJavaList(SearchUtil.ImgUrl.class);
                Integer total = jsonObject.getInteger("displayNum");
                List<String> urls = imgUrlList.stream().filter(Objects::nonNull)
                                              .map(SearchUtil.ImgUrl::getThumbURL).filter(Objects::nonNull)
                                              .collect(Collectors.toList());
                AtomicInteger i = new AtomicInteger(0);
                urls.forEach(imgUrl -> HttpUtil.downloadFile(imgUrl, path + keyword + File.separator + currentPage + "_" + i.getAndIncrement() + ".jpg"));
                int totalPage = total % 50 == 0 ? total / 50 : (total / 50) + 1;
                log.info("总页数:{},当前页:{}", totalPage, currentPage);
                return totalPage;
            }

        } catch (IOException e)
        {
            log.error("异常:{}", e.getMessage());
        }
        return 0;
    }

    public static void downloadImg(String path, List<String> keywords)
    {
        keywords.forEach(keyword -> {
            try
            {
                int totalPage = downloadImg(keyword, 0,path);
                for (int k = 1; k < Math.min(totalPage, 10); k++)
                {
                    downloadImg(keyword, k,path);
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e)
            {
                throw new RuntimeException(e);
            }
        });

    }

动物装饰