爬蟲記錄(1)——簡單爬取一個頁面的內容並寫入到文本中

1、爬蟲工具類,用來獲取網頁內容

package com.dyw.crawler.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * 爬蟲工具類
 * Created by dyw on 2017/9/1.
 */
public class CrawlerUtils {

    /**
     * 獲取html內容轉成string輸出。
     *
     * @param url url鏈接
     * @return 整個網頁轉成String字符串
     */
    public static String getHtml(String url) throws Exception {
        URL url1 = new URL(url);//使用java.net.URL
        URLConnection connection = url1.openConnection();//打開鏈接
        InputStream in = connection.getInputStream();//獲取輸入流
        InputStreamReader isr = new InputStreamReader(in);//流的包裝
        BufferedReader br = new BufferedReader(isr);

        String line;
        StringBuffer sb = new StringBuffer();
        while ((line = br.readLine()) != null) {//整行讀取
            sb.append(line, 0, line.length());//添加到StringBuffer中
            sb.append('\n');//添加換行符
        }
        //關閉各種流,先聲明的後關閉
        br.close();
        isr.close();
        in.close();
        return sb.toString();
    }

}

2、IO工具類,用來把獲取的html內容進行寫入到文件中

package com.dyw.crawler.util;

import java.io.File;
import java.io.FileOutputStream;

/**
 * IO工具類
 * Created by dyw on 2017/9/1.
 */
public class IOUtils {

    /**
     * 創建文件
     *
     * @param file File類型
     */
    public static void createFile(File file) throws Exception {
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (Exception e) {
            throw new Exception("創建文件的時候錯誤!", e);
        }
    }

    /**
     * 寫入String到file中
     *
     * @param content  寫入內容
     * @param fileName 寫入位置
     */
    public static void writeFile(String content, File fileName) throws Exception {
        FileOutputStream o;
        try {
            o = new FileOutputStream(fileName);
            o.write(content.getBytes("Utf-8"));
            o.close();
        } catch (Exception e) {
            throw new Exception("寫入文件的時候錯誤!", e);
        }
    }
}

3、main方法執行

package com.dyw.crawler.project;

import com.dyw.crawler.util.CrawlerUtils;
import com.dyw.crawler.util.IOUtils;

import java.io.File;

/**
 * 此包中的main方法
 * Created by dyw on 2017/9/1.
 */
public class Project {

    public static void main(String[] args) {
        //文件放置的路徑
        String path = "C:\\Users\\dyw\\Desktop\\crawler";
        //爬取的網站地址
        String url = "http://blog.csdn.net/juewang_love";
        String fileRealName = path + "/index.html";
        File file = new File(fileRealName);
        //創建文件
        try {
            IOUtils.createFile(file);
        } catch (Exception e) {
            throw new RuntimeException("創建文件失敗!", e);
        }
        //獲取內容
        String htmlContent = null;
        try {
            htmlContent = CrawlerUtils.getHtml(url);
        } catch (Exception e) {
            throw new RuntimeException("獲取內容失敗!", e);
        }
        //寫入內容
        try {
            IOUtils.writeFile(htmlContent, file);
        } catch (Exception e) {
            throw new RuntimeException("內容寫入文件失敗!", e);
        }
    }
}
發佈了62 篇原創文章 · 獲贊 80 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章