Crawling zol wallpaper with java
- First find the wallpaper page
Get ur: http://desk.zol.com.cn/showpic/1920x1080_4000_10.html
Then check the source code to find the information of the picture
<img src="https://desk-fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/01/0E/ChMkJ1bKwfWIK7FbAAiTgt3qkBkAALGggICrlgACJOa906.jpg" width="100" height="75">
After analyzing the url, we can find that the number 4000 is the id of this image, so we only need to divide the url into three parts, and the middle number is changed by cycling and then downloaded by thread.
- Create a method to get the image url
private String getImgUrl(String u) throws IOException { StringBuilder sb = new StringBuilder(); URL url = new URL(u); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 " + "(KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1"); InputStream in = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in,"gb2312")); String string = ""; while (null != (string = reader.readLine())) { sb.append(string); } string = sb.toString(); //Capture the URL of the image from the source code obtained Matcher matcher = Pattern.compile(imgReg).matcher(string); if (matcher.find()) { string = matcher.group(); string = string.substring(10, string.length() - 2); } in.close(); return string; }
- Then create a method to download pictures from the url obtained above
private void getImg(String imgUrl) throws IOException { URL url = new URL(imgUrl); InputStream in = url.openStream(); FileOutputStream fo = new FileOutputStream(new File("/image/" + this.number+".jpg")); byte[] by = new byte[1024]; int length = 0; System.out.println("Start downloading:" +this.number); while ((length = in.read(by, 0, by.length)) != -1) { fo.write(by, 0, length); } in.close(); fo.close(); }
Please refer to:
https://blog.csdn.net/qq_36781505/article/details/87453051