java爬取lol官网英雄名 与 故事

需要的maven坐标

  <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.12</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>


注意:不知道HttpClients可以先查看这篇文章
HttpClients

java程序

创建一个类来接收数据

package cn.guoke.pojo;

import lombok.Data;

@Data
public class Hero {
    private String heroId;
    private String Name;
    private String title;
    private String shortBio;

}

编写爬虫

package cn.guoke;


import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import 第一个爬虫程序.pojo.Hero;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HeroUtils  {

    /**
     * @return 返回HeroId
     */
    public static List<Integer> getHeroId()  {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js");
        ArrayList<Integer> integers = new ArrayList<Integer>();
        try {
            CloseableHttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode()==200){
                HttpEntity entity = response.getEntity();
                String s = EntityUtils.toString(entity, "utf-8");
                JSONObject jsonObject = new JSONObject(s);
                JSONArray hero = jsonObject.getJSONArray("hero");
                for (int i = 0; i < hero.length(); i++) {
                    int heroId = hero.getJSONObject(i).getInt("heroId");
                    integers.add(heroId);
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return integers;
    }


    /**
     *
     * @return 返回爬链接
     */
    public static List<String> getLolUrl(){
        //"https://game.gtimg.cn/images/lol/act/img/js/hero/1.js"
        ArrayList<String> strings = new ArrayList<String>();
        for (Integer integer : getHeroId()) {
            String s = integer.toString();
            String s1 = "https://game.gtimg.cn/images/lol/act/img/js/hero/"+s+".js";
            strings.add(s1);
//            System.out.println(s1);
        }

        return strings;
    }

    public static List<Hero> getAllHero(){
        CloseableHttpClient httpClient = HttpClients.createDefault();

        ArrayList<Hero> heroes = new ArrayList<Hero>();
        for (String s : getLolUrl()) {
            HttpGet httpGet = new HttpGet(s);
            try {
                CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
                if (httpResponse.getStatusLine().getStatusCode()==200){
                    HttpEntity entity = httpResponse.getEntity();
                    String s1 = EntityUtils.toString(entity, "utf-8");
                    JSONObject jsonObject = new JSONObject(s1);
                    JSONObject hero = jsonObject.getJSONObject("hero");
                    String shortBio = hero.getString("shortBio");
                    String name = hero.getString("name");
                    String title = hero.getString("title");
                    String heroId = hero.getString("heroId");
                    Hero hero1 = new Hero();
                    hero1.setName(name);
                    hero1.setShortBio(shortBio);
                    hero1.setTitle(title);
                    hero1.setHeroId(heroId);
                    heroes.add(hero1);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return heroes;
    }


    public static void main(String[] args) {
        List<Hero> allHero = getAllHero();
        System.out.println(allHero);

    }
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值