爬取指定网页图片
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownLoadImage {
public static void downLink() throws IOException {
File input = new File("C:\\Users\\18404\\Desktop\\1.html");
Document doc = Jsoup.parse(input, "UTF-8", "https://mp.weixin.qq.com/s/fUgISHL-ZrcI1efDiiZR2w");
Element content = doc.getElementById("js_content");
Elements links = content.getElementsByTag("img");
System.out.println(links.size());
for (Element link : links) {
String linkHref = link.attr("data-src");
System.out.println(linkHref);
new DownLoadImage().downImage(linkHref);
}
}
public void downImage(String strUrl) throws IOException {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
conn.connect();
InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = inStream.read(buf)) != -1) {
outStream.write(buf, 0, len);
}
inStream.close();
outStream.close();
int max=100000,min=1;
int ran2 = (int) (Math.random()*(max-min)+min);
System.out.println(ran2);
File file = new File("C:\\Users\\18404\\Desktop\\1\\"+ran2+".jpg");
FileOutputStream op = new FileOutputStream(file);
op.write(outStream.toByteArray());
op.close();
}
public static void main(String[] args) throws IOException {
downLink();
}
}