这里使用的是OKHttp发送请求,记得添加依赖
测试类
public static String parseConfig(String fileName) {
File jsonFile = new File(fileName);
try (Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8")) {
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) throws IOException {
//爬取英雄信息文件
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request rest1 = new Request.Builder()
.url("https://pvp.qq.com/web201605/js/herolist.json")
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36")
.build();
Response response1 = client.newCall(rest1).execute();
byte[] body = response1.body().bytes();
System.out.println(response1);
File f = new File("D:/wzry.json");
if(!f.exists()) {
f.createNewFile();
}
InputStream in = null;
OutputStream out = null;
in = new ByteArrayInputStream(body);
out = new FileOutputStream(f);
byte []bt = new byte[1024];
int length=0;
while( (length = in.read(bt)) !=-1) {
//一次性写入文件a中
out.write(bt,0,length);
}
if(null!=out) {
out.close();
}
//创建储存图片的文件夹
File file = new File("D:/图片/wzry");
if(!file.exists()) {
if(!file.isDirectory()){
file.mkdir();
}
}
String parseConfig = FileController.parseConfig(f.getAbsolutePath());//读取英雄信息文件
//转换成实体集合
List parse = (List)JSONArray.parse(parseConfig);
List<wzry> ls=new ArrayList<>();
for (int i = 0; i < parse.size(); i++) {
wzry w = JSONObject.parseObject(parse.get(i).toString(),wzry.class);
ls.add(w);
}
//遍历集合,开始爬取
for (int j = 0; j < ls.size(); j++) {
wzry wzry = ls.get(j);
File file1 = new File("D:/图片/wzry/"+wzry.getCname());
if(!file1.exists()) {
if(!file1.isDirectory()){
file1.mkdir();
}
}
if(wzry.getSkin_name()==null){
continue;
}
wzry.setSkin_name(wzry.getSkin_name().replace("|", ","));
String[] split = wzry.getSkin_name().split(",");
for (int i = 0; i < split.length; i++) {
int index=i+1;
Request rest = new Request.Builder()
.url("https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/"+wzry.getEname()+"/"+wzry.getEname()+"-bigskin-"+index+".jpg")
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36")
.build();
Response response = client.newCall(rest).execute();
body = response.body().bytes();
System.out.println(response);
File dest = new File("D:/图片/wzry/"+wzry.getCname()+"/"+split[i]+".jpg");
if(!dest.exists()) {
dest.createNewFile();
}
in = new ByteArrayInputStream(body);
out = new FileOutputStream(dest);
bt = new byte[1024];
length=0;
while( (length = in.read(bt)) !=-1) {
//一次性写入文件a中
out.write(bt,0,length);
}
if(null!=out) {
out.close();
}
}
}
json转换用到的实体类
package entity;
public class wzry {
private int new_type;
private String title;
private int pay_type;
private String skin_name;
private int moss_id;
private int ename;
private int hero_type;
private String cname;
public void setNew_type(int new_type) {
this.new_type = new_type;
}
public int getNew_type() {
return new_type;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setPay_type(int pay_type) {
this.pay_type = pay_type;
}
public int getPay_type() {
return pay_type;
}
public void setSkin_name(String skin_name) {
this.skin_name = skin_name;
}
public String getSkin_name() {
return skin_name;
}
public void setMoss_id(int moss_id) {
this.moss_id = moss_id;
}
public int getMoss_id() {
return moss_id;
}
public void setEname(int ename) {
this.ename = ename;
}
public int getEname() {
return ename;
}
public void setHero_type(int hero_type) {
this.hero_type = hero_type;
}
public int getHero_type() {
return hero_type;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCname() {
return cname;
}
}
依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
最后提醒大家,爬取数据是会对服务器造成不小压力的,请注意分寸。