当前位置:网站首页>Using jsup to extract images from interfaces

Using jsup to extract images from interfaces

2022-06-26 04:04:00 Lie down and count the stars

Import dependence

 <!-- Reptiles -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.10.2</version>
        </dependency>

Use hutool Tool parsing json data

 @GetMapping("/pachou")
    @ApiOperation(" Reptiles ")
    public String teamMessage()  {
    

// s = HttpUtil.get("https://china.nba.cn/stats2/season/conferencestanding.json?");
        s = HttpUtil.get("https://china.nba.cn/stats2/league/playerlist.json");

        return s;
    }

Extract and save to disk

  @PostMapping("/addBatch2")
    @ApiOperation(value = " Batch insert ")
    public Result addBatch2(@RequestBody String str) throws IOException, InterruptedException {
    

        JSONArray jsonArray = JSONArray.parseArray(str);
        FileOutputStream outputStream =null;
// FileInputStream inputStream =null;
        InputStream inputStream=null;
        for (int i = 0; i < jsonArray.size(); i++) {
    
            JSONObject jsonObject = JSONObject.parseObject(jsonArray.get(i).toString());
            String playerProfile = jsonObject.getString("playerProfile");
            String teamProfile = jsonObject.getString("teamProfile");
            JSONObject object = JSONObject.parseObject(playerProfile);
            JSONObject object2 = JSONObject.parseObject(teamProfile);
// System.out.println(object.getString("displayName"));
// System.out.println(object2.getString("city"));
            String playerImg = object.getString("playerId");
            String displayName = object.getString("displayName");
            System.out.println(playerImg);
            try {
    
                String url="https://res.nba.cn/media/img/players/head/260x190/"+playerImg+".png";
                URL imgsrcUrl = new URL(url);
                URLConnection connection = imgsrcUrl.openConnection();
                inputStream = connection.getInputStream();
                String path="F:\\chenchao\\NBA\\"+displayName+".png";
                outputStream = new FileOutputStream(path);
                byte[] bytes = new byte[1024];
                int len = 0;
                while((len=inputStream.read(bytes))!=-1){
    
                    outputStream.write(bytes,0,len);

                }

                System.out.println(" Download complete ");
            } catch (IOException e) {
    
                e.printStackTrace();
            } catch (Exception e) {
    
                e.printStackTrace();
            }

        }

        return Result.success();
    }

effect
 Insert picture description here

原网站

版权声明
本文为[Lie down and count the stars]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260359265316.html