Java爬虫Jsoup爬取必应壁纸
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Scanner;
public class DIYMain3 {
public static void main(String[] args) throws Exception {
int page = 1;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要下载多少页");
int howMuchPage = scanner.nextInt();
while (page <= howMuchPage) {
Document document = Jsoup.connect("https://bing.ioliu.cn/?p=" + page + "").get();
Elements elements = document.select("a.mark");
int i = 1;
for (Element element : elements) {
String href = element.attr("href");
String newURL = "https://bing.ioliu.cn/" + href + "";
Document document1 = Jsoup.connect(newURL).get();
String imageSrc = document1.select("img.target.progressive__img.progressive--not-loaded").attr("data-progressive");
try {
byte[] bytes = Jsoup.connect(imageSrc).ignoreContentType(true).execute().bodyAsBytes();
File filePath = new File("C://bring2//");
if (!filePath.exists())
filePath.mkdir();
FileOutputStream fileOutputStream = new FileOutputStream("" + filePath + "/第" + page + "页-第" + i + "张.jpg");
fileOutputStream.write(bytes);
} catch (Exception e) {
continue;
}
System.out.println("第" + page + "页-第" + i + "张.jpg正在下载.......");
i++;
}
page++;
}
System.out.println("下载完成!");
}
}