【Java项目】讲讲我用Java爬虫获取LOL英雄数据与图片
以下是一个简化的Java爬虫代码示例,用于获取LOL英雄数据和图片,并保存到本地。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class LeagueOfLegendsCrawler {
private static final String HERO_INFO_URL = "http://lol.esportsentertainment.com/champion/";
private static final String IMAGE_URL_PREFIX = "http://ddragon.leagueoflegends.com/cdn/9.2.1/img/champion/";
private static final String SAVE_DIR = "LOLEngines";
public static void main(String[] args) {
for (int i = 1; i <= 118; i++) { // 假设我们只爬取前118个英雄,实际可以根据实际网站结构爬取所有
String heroId = String.valueOf(i);
String heroUrl = HERO_INFO_URL + heroId;
String imageUrl = IMAGE_URL_PREFIX + heroId + ".png";
downloadHeroData(heroUrl, imageUrl, heroId);
}
}
private static void downloadHeroData(String heroUrl, String imageUrl, String heroId) {
try {
Document doc = Jsoup.connect(heroUrl).get();
Elements nameElements = doc.select("title");
String heroName = nameElements.get(0).text().replace(" - League of Legends Esports & Entertainment", "");
// 下载图片
Path imageSavePath = downloadImage(imageUrl, heroName);
System.out.println("Hero Name: " + heroName);
System.out.println("Image Saved at: " + imageSavePath);
} catch (IOException e) {
e.printStackTrace();
}
}
private static Path downloadImage(String imageUrl, String fileName) {
try {
File tempFile = new File(SAVE_DIR + File.separator + fileName + ".png");
Path path = Paths.get(tempFile.getAbsolutePath());
if (tempFile.exists()) {
System.out.println("File already exists: " + path);
return path;
}
URL url = new URL(imageUrl);
Files.copy(url.openStream(), path);
return path;
} catch (IOException e) {
e.printStackTrace();
}
retur
评论已关闭