爬虫记录(1)——简单爬取一个页面的内容并写入到文本中

本文介绍了一个简单的Java爬虫项目,包括爬虫工具类用于获取指定网页的内容,并将其转换为字符串;IO工具类用于将获取的HTML内容写入本地文件。通过具体的main方法演示了从指定网址抓取数据并保存的过程。

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);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值