爬虫记录(1)——简单爬取一个页面的内容并写入到文本中
2017-09-01 17:24
435 查看
1、爬虫工具类,用来获取网页内容
2、IO工具类,用来把获取的html内容进行写入到文件中
3、main方法执行
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); } } }
相关文章推荐
- 爬虫记录(2)——简单爬取一个页面的图片并保存
- python (1)一个简单的爬虫: python 在windows下 创建文件夹并写入文件
- 一个简单的写入文件内容的小demo
- 一个简单的用javascript实现的页面内容过滤显示小程序源码
- 一个简单的用javascript实现的页面内容过滤显示小程序源码
- 简易爬虫抓取网页内容并写入文本
- 一个简单的python爬虫,抓取单个页面的图片
- 通过js简单实现将一个文本内容转译成加密文本
- 新闻发布程序(1):建立一个通过前台输入文本内容的简单网页程序
- 一个简单的使用代理访问百度页面内容的python脚本
- 一个简单的网站页面点击记录ver0.1
- 一个简单的爬虫程序(爬取百度百科关于python的一千个页面)
- 一个简单H5投票页面的踩坑记录
- javaweb之Session简单例子(两个页面一个存数据,一个取数据)和session的一些基本内容
- js 页面上有五个checkbox按钮,以及一个textbox文本框。当勾选文本框时textbox文本框内的内容显示为当前勾选了几个checkbox,并且显示相应的checkbox文本内容。
- 4-1:编写一个简单的留言簿,写入留言提交后显示留言内容。
- 一个抓取知乎页面图片的简单爬虫
- java对txt文本文件的复制粘贴(对文件内容进行了简单的修改)以行为一个处理单位
- java.nio将一个文件的内容写入到另一个的文件简单例子
- C# 实现简单打印(二)-打印一个文本文档,打印的内容是多行的