JAVA爬虫---LOL各英雄图片(含皮肤)下载

news/2024/11/29 12:39:36/

pom依赖

 		<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.2</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.69</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

实现代码

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;
import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;/*** @author chunyang.leng* @date 2021/1/31 7:57 下午*/
public class LOLImagesDownload {private static String listURL = "http://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js";private static String imageInfoURL = "http://game.gtimg.cn/images/lol/act/img/js/hero/{0}.js";private static List<Future<String>> list = Collections.synchronizedList(new ArrayList<>());private static final Logger logger = LoggerFactory.getLogger(LOLImagesDownload.class);/*** 需要调整的地方,配置你需要保存的路径,以 / 结束*/private static String saveDir = "/Users/lcy/Pictures/LOL壁纸/";public static void main(String[] args) {RestTemplate restTemplate = getRestTemplate();AsyncTaskExecutor threadPool = getThreadPool();ResponseEntity<String> responseEntity = restTemplate.getForEntity(listURL, String.class);String stringBody = responseEntity.getBody();JSONObject body = JSONObject.parseObject(stringBody);JSONArray heroList = body.getJSONArray("hero");for (int i = 0; i < heroList.size(); i++) {JSONObject hero = heroList.getJSONObject(i);String heroId = hero.getString("heroId");String name = hero.getString("name");File dir = new File(saveDir + name);if (!dir.exists()) {dir.mkdirs();}String imageUrl = MessageFormat.format(imageInfoURL, heroId);ResponseEntity<String> forEntity = restTemplate.getForEntity(imageUrl, String.class);String imageInfoBody = forEntity.getBody();JSONObject imageInfo = JSONObject.parseObject(imageInfoBody);JSONArray skins = imageInfo.getJSONArray("skins");for (int i1 = 0; i1 < skins.size(); i1++) {int finalI = i1;Future<String> future = threadPool.submit(() -> {JSONObject jsonObject = skins.getJSONObject(finalI);String mainImgUrl = jsonObject.getString("mainImg");if (StringUtils.isEmpty(mainImgUrl)) {return null;}String fileName = jsonObject.getString("name").replaceAll("\\s", "-");File image = new File(dir + "/" + fileName + ".jpg");if (!image.exists()) {image.createNewFile();} else {image.delete();image.createNewFile();}HttpHeaders headers = new HttpHeaders();ResponseEntity<byte[]> response = restTemplate.exchange(mainImgUrl,HttpMethod.GET,new HttpEntity<byte[]>(headers),byte[].class);byte[] result = response.getBody();ImageOutputStream imageOutputStream = new FileImageOutputStream(image);imageOutputStream.write(result);imageOutputStream.flush();imageOutputStream.close();return fileName;});list.add(future);}}list.forEach(o -> {if (o != null) {try {final String s = o.get();if (o != null) {logger.info(s + ":下载成功");}} catch (Exception e) {logger.error("下载出错", e);}}});}public static AsyncTaskExecutor getThreadPool() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();//获取到服务器的cpu内核int i = Runtime.getRuntime().availableProcessors();System.out.println("cpu core :" + i);//核心池大小executor.setCorePoolSize(i * 2);//最大线程数executor.setMaxPoolSize(i * 4);//线程空闲时间executor.setKeepAliveSeconds(100000);//线程前缀名称executor.setThreadNamePrefix("pool-");//配置拒绝策略executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());executor.afterPropertiesSet();return executor;}private static RestTemplate getRestTemplate() {return new RestTemplate(new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().build()));}
}

效果截图
在这里插入图片描述
在这里插入图片描述


http://www.ppmy.cn/news/841634.html

相关文章

利用Scrapy框架爬取LOL皮肤站高清壁纸

利用Scrapy框架爬取LOL皮肤站高清壁纸 Lan 2020-03-06 21:22 81 人阅读 0 条评论 成品打包&#xff1a;点击进入 代码&#xff1a; 爬虫文件 # -*- coding: utf-8 -*- import scrapy from practice.items import PracticeItem from urllib import parseclass LolskinSpider(s…

我用Python一键爬取了:所有LOL英雄皮肤壁纸!

人生苦短&#xff0c;快学Python&#xff01; 今天是教使用大家selenium&#xff0c;一键爬取LOL英雄皮肤壁纸。 第一步&#xff0c;先要进行网页分析 一、网页分析 进入LOL官网后&#xff0c;鼠标悬停在游戏资料上&#xff0c;等出现窗口&#xff0c;选择资料库&#xff0c;…

python爬取千图网_python爬取lol官网英雄图片代码

python爬取lol官网英雄图片代码可以帮助用户对英雄联盟官网平台的皮肤图片进行抓取&#xff0c;有很多喜欢lol的玩家们想要官方的英雄图片当作自己的背景或者头像&#xff0c;可以使用这款软件为你爬取图片资源&#xff0c;操作很简单&#xff0c;设置一些保存路径就可以将图片…

抓取 LOL 官网墙纸实现

闲来无事&#xff08;蛋疼&#xff09;&#xff0c;随手实现了下 Controller 代码 <?php namespace App\Http\Controllers\Image;use App\Http\Controllers\Controller; use Illuminate\Filesystem\Filesystem as Fs;class ImageController extends Controller {public sta…

nodejs http内置模块使用

/** * 创建一个 HTTP 服务&#xff0c;端口为 9000&#xff0c;满足如下需求 * GET /index.html 响应 page/index.html 的文件内容 * GET /css/app.css 响应 page/css/app.css 的文件内容 * GET /images/logo.png 响应 page/images/logo.png 的文件内…

【ONNXRuntime】python找不到指定的模块:onnxruntime\capi\onnxruntime_providers_shared.dll

问题&#xff1a; 使用源码推理的时候onnruntime 能够使用cuda&#xff0c;但是使用pyinstaller导出包之后&#xff0c;推理就会出现找不到 onnxruntime\capi\onnxruntime_providers_shared.dll错误。 原因&#xff1a;原因就是运行的时候没有找到动态链接库呗。 解决方法&a…

关于solidworks软件的显卡驱动

运行像solidworks这样的3D设计软件&#xff0c;装配体稍微复杂&#xff0c;一般的GTX游戏卡就无法胜任&#xff0c;这时候需要专门的图形卡加持。个人可以针对自己的装配体规模选择合适的图形卡&#xff0c;一般500-1000个零件以内的装配体&#xff0c;用丽台K4000完全可以胜任…

HI3798MV200驱动移植

目录 1.UBOOT配置修改方法 2.由EMMC启动改为SPI NAND FLASH 启动 3.网络调试 4.PHY复位 5.内核起来网络不通 6.增加RTC 7.PHY 灯ACT LINK 问题 8.PHY link状态查询 9.ETH0 网络状态灯修改 1.UBOOT配置修改方法 需要对应版本的HITOOL&#xff0c;个人也是废了很大劲&a…