-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleCrawler.java
169 lines (158 loc) · 4.6 KB
/
SimpleCrawler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package java_crawler;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Simple Crawler: download picture from baidu
*
* It is just feasible to http://www.baidu.com
*
* @author 唐龙
*
*/
public class SimpleCrawler {
static String http;
public static void main(String[] args) throws IOException{
String url = "http://www.baidu.com";
http = url.split(":")[0] + "://www";
// get result from url
String result = urlResult(url);
// write data into a html file
String fileHtml = "src/java_crawler/baidu.html";
fileIn(fileHtml,result);
// write data into a txt file
String fileTxt = "src/java_crawler/baidu.txt";
fileIn(fileTxt,result);
// get image url
String regex = "src=.*>";
List<String> urList = regExpStr(regex, result);
// download and save picture
String path = "src/java_crawler/";
for(int i=0,len=urList.size();i<len;i++) {
String str = urList.get(i);
String urlp = str.substring(str.indexOf('.'), str.lastIndexOf('.'));
pictureDownload(http + urlp + ".png",path + i + ".png");
}
}
/**Get result according to url*/
private static String urlResult(String urlStr) {
// 存储网页内容
StringBuilder sb = new StringBuilder();
// 缓冲字符输入流
BufferedReader in = null;
try {
// 将string转成url对象
URL url = new URL(urlStr);
// 初始化到url的连接
URLConnection connection = url.openConnection();
// 连接
connection.connect();
// 初始化 BufferedReader输入流来读取URL的响应,编码格式“utf-8”
in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
// 用来临时存储抓取到的每一行的数据
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line);//遍历抓取到的每一行数据并进行存储
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常:" + e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
System.out.println("Get html success...");
return sb.toString().replaceAll(">", ">\n"); // format the result
}
/**
* file write
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
* */
private static void fileIn(String fileName, String result) throws FileNotFoundException, UnsupportedEncodingException {
String encode = fileName.split("\\.")[1].equals("html")? "utf-8":"gbk";
FileOutputStream fos = new FileOutputStream(fileName);
OutputStreamWriter osw = new OutputStreamWriter(fos,encode);
try (PrintWriter pw = new PrintWriter(new BufferedWriter(osw));) {
pw.println(result.toString());
pw.flush();//刷新缓存
}
}
/**
* Get image urls
*
* @param regex
* @param result
* @return
*/
private static List<String> regExpStr(String regex, String result) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(result);
List<String> list = new ArrayList<String>();
if(m.find()) {
list.add(m.group(0));
}
System.out.println("Get image url success...");
return list;
}
/**
* Download pictures from urls and save them
*
* @param urlStr
* @param dir
* @throws IOException
*/
private static void pictureDownload(String urlStr,String path) throws IOException {
URL url = null;
DataInputStream dis = null;
FileOutputStream fos = null;
ByteArrayOutputStream baos = null;
try {
url = new URL(urlStr);
dis = new DataInputStream(url.openStream());
fos = new FileOutputStream(new File(path));
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dis.read(buffer)) > 0) {
baos.write(buffer, 0, length);
}
fos.write(baos.toByteArray());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(dis != null) {
dis.close();
}
if(fos != null) {
fos.close();
}
if(baos != null) {
baos.close();
}
}
System.out.println("Picture download success...");
}
}