先放图
我图没了,说我sq,直接看代码吧,原来网址也不行…
上代码
public static void main(String[] args) {
String url = "lmth.utiem/moc.0004niw.www//:ptth";
getMoreUrls(url);
}
private static void getMoreUrls(String url) {//获取当前页面的 更多按钮的url
List moreUrlList = new ArrayList();
Document document = null;
try {
document = Jsoup.connect(url).timeout(6000).get();
} catch (IOException e) {
e.printStackTrace();
}
if (document != null) {
Elements elements = document.getElementsByTag("a");
for (Element e: elements) {
if (e.attr("href").contains("tag")){
String moreUrl = e.attr("href");
int UrlSize = getMoreUrlSize(moreUrl);
for (int i = 1; i <= UrlSize; i++) {
String minUrl = moreUrl.substring(0,33)+i+".html";
moreUrlList.add(minUrl);
}
}
}
for (int i = 0; i < moreUrlList.size(); i++) {
down(moreUrlList.get(i).toString());
}
}
}
private static int getMoreUrlSize(String url) { //获取更多按钮的数量
Document document = null;
try {
document = Jsoup.connect(url).timeout(6000).get();
} catch (IOException e) {
e.printStackTrace();
}
List lastList = new ArrayList();
if (document != null) {
Elements elements = document.getElementsByClass("num");
for (Element e: elements) {
if (e.attr("rel")!= null && !e.attr("rel").equals("")){
lastList.add(e.html());
}
}
}
return Integer.parseInt(lastList.get(lastList.size()-1).toString());
}
private static void down(String url) {//获取当前页面 所有图片的url
Document document = null;
try {
document = Jsoup.connect(url).timeout(6000).get();
} catch (IOException e) {
e.printStackTrace();
}
if (document != null) {
Elements elements = document.getElementsByTag("a");
for (Element e: elements) {
if (e.attr("href").contains("meinv") && e.attr("href").contains("http")
&& !e.attr("href").contains("tag")){
String URL = e.attr("href");
String TITLE = e.children().first().attr("alt");
getNum(URL,TITLE);
}
}
}
}
private static void getNum(String url,String title) {//获取某一系列的url
Document document = null;
try {
document = Jsoup.connect(url).timeout(6000).get();
} catch (IOException e) {
e.printStackTrace();
}
if (document != null) {
int num = Integer.parseInt(document.getElementsByTag("em").first().html());
for (int i = 1; i <= num; i++) {
String URL = url.split(".html")[0]+"_"+i+".html";
getLarge(URL,title,i);
}
}
}
private static void getLarge(String url,String title,int num) {//通过url 获取当前页面的图片的大图src
Document document = null;
try {
document = Jsoup.connect(url).timeout(6000).get();
} catch (IOException e) {
e.printStackTrace();
}
if (document != null) {
Elements elements = document.getElementsByClass("pic-large");
String URL =elements.get(0).attr("url");
save(URL,num,title);
}
}
private static void save(String url,int title,String floder){ //保存到电脑
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(url).openConnection();
if (connection.getResponseCode() == 200) {
InputStream put = connection.getInputStream();
String path = "D:\\桌面\\img\\"+floder;
File file0 = new File(path);
file0.mkdir();
File file = new File(path+"\\"+title+".jpg");
if (!file.exists()) file.createNewFile();
OutputStream out = new FileOutputStream(file);
byte[] buff = new byte[1024];
int stream;
while ((stream = put.read(buff)) > 0) out.write(buff, 0, stream);
put.close();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(floder+title+"保存成功");
}