简单的爬虫案例
2019-07-09 22:33 更新
简单的爬虫案例: 将百度的首页爬取下来并保存在文件中。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
public class Test {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
URL url = new URL("http://www.baidu.com");
BufferedReader bReader =
new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
BufferedWriter bWriter =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream("baidu.html")));
String msg = null;
while((msg = bReader.readLine()) != null) {
// System.out.println(msg);
bWriter.append(msg + "\n");
}
bWriter.close();
bReader.close();
}
}
以上内容是否对您有帮助:
更多建议: