簡(jiǎn)單的爬蟲(chóng)案例

2019-07-09 22:33 更新

簡(jiǎn)單的爬蟲(chóng)案例: 將百度的首頁(yè)爬取下來(lái)并保存在文件中。

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();
    }
}
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)