import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
public class CrawlerPic {
private static Set<String> imgs = new HashSet<String>();
public static void main(String[] args) throws IOException {
URL url = new URL("https://www.qq.com/");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String s;
while ((s = br.readLine()) != null) {
System.out.println(s);
if (s.indexOf("<img") != -1) {
int i = s.indexOf("<img");
int j = i;
while (true) {
char ch = s.charAt(j++);
if (ch == '>')
break;
}
String img = s.substring(i, j);
img = img.substring(1, img.length() - 1);
String[] imgs = img.split("\" ");
for (String string : imgs) {
i = 0;
while (i != -1) {
i = string.indexOf("src=");
if (i != -1) {
img = string.substring(i + 5);
// writeToFile(img);
}
if ((i = string.indexOf("data-src=")) != -1) {
img = string.substring(i + 10);
// writeToFile(img);
}
if ((i = string.indexOf("data-original=")) != -1) {
img = string.substring(i + 15);
// writeToFile(img);
}
i = -1;
}
}
}
}
System.out.println(imgs);
}
private static void writeToFile(String img) {
img = img.trim();
if (img.length() == 0) {
return;
}
if (imgs.contains(img))
return;
imgs.add(img);
if (img.charAt(img.length() - 1) == '"')
img = img.substring(0, img.length() - 1);
String fileName = img.substring(img.lastIndexOf("/") + 1);
if (fileName.indexOf(".") == -1) {
String[] url = img.split("[/]");
fileName = url[url.length - 2] + "_" + url[url.length - 1] + ".png";
}
// System.out.println(fileName);
InputStream in = null;
FileOutputStream out = null;
try {
if (!img.startsWith("http"))
img = "https:" + img;
URL url = new URL(img);
in = url.openStream();
out = new FileOutputStream("E:/tmp/" + fileName);
int i = 0;
while ((i = in.read()) != -1) {
out.write(i);
}
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
if (out != null)
try {
out.close();
} catch (Exception e) {
}
}
}
}