Java抓取网页图片

2022-06-13 00:00:00 网页 图片 抓取

java抓取网页图片

无版权图片网站:unsplash
https://unsplash.com/

 public static void test2 () throws IOException { 
 	// 请求链接
 	String url = "https://unsplash.com/napi/topics/wallpapers/photos?page=1&per_page=20";
    BufferedReader in = null;
    try { 
        URL realUrl = new URL(url);
        // 打开和URL之间的连接
        URLConnection connection = realUrl.openConnection();
        // 设置通用的请求属性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        // user-agent添加,模拟用户访问
        connection.setRequestProperty("user-agent",
                        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 建立实际的连接
         connection.connect();
         
        // 定义 BufferedReader输入流来读取URL的响应
        in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
        String line;
        StringBuffer buffer = new StringBuffer();
        while ((line = in.readLine()) != null) { 
            buffer.append(line);
        }
        // 字符串转为JSON对象
        // 数组使用JSONArray , 对象格式使用JSONObject
        JSONArray ct = new JSONArray(buffer.toString());

        for (int i = 0; i < ct.length(); i++) { 
			JSONObject obj = ct.getJSONObject(i);

            // width < height
            if ((Integer) obj.get("width") > (Integer) obj.get("height") ||
                   (Integer) obj.get("width") == (Integer) obj.get("height")) { 
                 continue;
            }
            
            // obj.get("属性名") 获取想要的数据
            String urls = obj.get("urls").toString();
			

            } catch (Exception e) { 
                e.printStackTrace();
            }
            // 使用finally块来关闭输入流
            finally { 
                try { 
                    if (in != null) { 
                        in.close();
                    }
                } catch (Exception e2) { 

                }
       }
 }
 

网站返回协议,根据需要抓取图片链接《Java抓取网页图片》

    原文作者:洋哥登陆
    原文地址: https://blog.csdn.net/Peanutfight/article/details/119654732
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章