1 package downloadpic;
2
3 import org.jsoup.Jsoup;
4 import org.jsoup.nodes.Document;
5 import org.jsoup.select.Elements;
6
7 import javax.print.Doc;
8 import java.io.File;
9 import java.io.FileOutputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.net.URL;
13 import java.net.URLConnection;
14
15 public class donloadBeauty extends constantString {
16 public static int target = 18;
17 public static void main(String argv[]) throws IOException {
18 Document document = Jsoup.connect(new constantString().url_root).get();
19 //System.out.println(document.text());
20 Elements number_beauty_elements = document.getElementsByClass("p_title");
21 int number_beauty = number_beauty_elements.size();
22 System.out.println(number_beauty);
23
24 //to get all the name and url
25 String[] beauty = new String[number_beauty];
26 String[] beauty_title = new String[number_beauty];
27 for (int i = 0; i < number_beauty; i++) {
28 beauty[i] = number_beauty_elements.get(i)
29 .select("a[href]").attr("href").toString();
30 beauty_title[i] = number_beauty_elements.get(i)
31 .text();
32 System.out.println(i + ":\tfile name:" + beauty_title[i] + "\n" + "url is: " + beauty[i]);
33 }
34
35 //test just one url
36 //to get user name
37 String user_home = "/home/" + System.getProperty("user.name") + "/Beauty/";
38 //to creat folder
39 String file_full_path = user_home + beauty_title[target].replaceAll("/", "");
40 File file = new File(file_full_path);
41 if (file.exists()) {
42 System.out.println("file already exits");
43 } else {
44 file.mkdirs();
45 }
46
47 //get first element content
48 Document document1_beauty = Jsoup.connect(beauty[target]).get();
49 String pic_number_string = null;
50 //pic_number.startsWith("图片数量:");
51 //int pic_number = Integer.parseInt(pic_number_string);
52 Elements elements_p = document1_beauty.getElementsByClass("c_l").select("p");
53 int size_p = elements_p.size();
54 int position = 0;
55 for(int n = 0; n < size_p; n ++){
56 pic_number_string = elements_p.get(n).text().toString();
57 if(pic_number_string.endsWith("张")){
58 position = n;
59 break;
60 }
61 }
62 pic_number_string = document1_beauty.getElementsByClass("c_l")
63 .select("p").get(position).text().toString();
64 System.out.println(pic_number_string);
65
66 //to get the number
67 int index_last_0 = pic_number_string.lastIndexOf(" ");
68 String temp_string = pic_number_string.substring(0, index_last_0);
69 System.out.println(temp_string);
70 int index_last_1 = temp_string.lastIndexOf(" ");
71 //System.out.println(pic_number_string.substring(index_last_1));
72
73 String number_string = pic_number_string.substring(index_last_1, index_last_0)
74 .replaceAll(" ", "");
75 System.out.println(number_string);
76 int pic_number = Integer.parseInt(number_string);
77 System.out.println("the pic number is: " + pic_number);
78
79 //get specific url
80 Document document_1 = Jsoup.connect(beauty[target]).get();
81 Elements elements = new Elements();
82 int i = document_1.getElementsByClass("content").select("img").size();
83
84 System.out.println("" + i);
85 for (int j = 0; j < i; j++) {
86 String href = document_1.getElementsByClass("content").select("img")
87 .get(j).attr("src").toString();
88 System.out.println(j + "," + "the url is:" + href);
89 }
90 String url_people = document_1.getElementsByClass("content").select("img")
91 .get(0).attr("src").toString();
92 System.out.println(url_people);
93
94 //to get all the pic file url
95 int pic_index_last_0 = url_people.lastIndexOf("/");
96 String temp_string_temp = url_people.substring(0, pic_index_last_0);//to get https://mtl.gzhuibei.com/images/img/20779
97 String userful_string = url_people.substring(pic_index_last_0);//to get 1.jpg
98 int to_get_dot = userful_string.lastIndexOf(".");
99 String extension_string = userful_string.substring(to_get_dot);//to get .jpg
100 System.out.println(extension_string);
101 System.out.println(userful_string);
102 System.out.println(temp_string_temp);
103 String[] beauty_full_url = new String[pic_number];
104 for (int k = 0; k < pic_number; k++) {
105 beauty_full_url[k] = temp_string_temp + "/" + (k+1) + extension_string;
106 System.out.println(k +"\t" + beauty_full_url[k]);
107 }
108
109 //to download the file
110 int bytesum = 0;
111 int byteread = 0;
112 System.out.println(file_full_path + "\turl is:\t" + target + ":\t"+ beauty[target]);
113 //System.out.println(target + beauty[target]);
114 for(int l = 0; l < pic_number; l ++){
115 URL url = new URL(beauty_full_url[l]);
116 String file_name = l + 1 + extension_string;
117 String file_full_name = file_full_path + "/" + file_name;
118 File file_new = new File(file_full_name);
119 if(file_new.exists()){
120 System.out.println("file already exists!");
121 }else{
122 try{
123 URLConnection conn = url.openConnection();
124 InputStream inStream = conn.getInputStream();
125 FileOutputStream fs = new FileOutputStream(file_full_name);
126
127 byte[] buffer = new byte[2048];
128 int length;
129 while ((byteread = inStream.read(buffer)) != -1) {
130 bytesum += byteread;
131 //System.out.println(bytesum / 1024);
132 fs.write(buffer, 0, byteread);
133 }
134 System.out.println("**********" + file_name + "**********");
135
136 } catch (Exception e) {
137 e.printStackTrace();
138 }
139 }
140 }
141 }
142 }