使用Java爬虫得到CSDN博客信息并保存(一)
来源:互联网 发布:json查看 编辑:程序博客网 时间:2024/04/26 17:10
1.
需求:
上一篇文章已经可以得到博客中的信息,这篇博客将示范把信息以txt文件的形式保存到本地
2.
代码实现:
package cn.test12.WebRobot04;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStreamWriter;import java.net.URL;import java.net.URLConnection;/* * 创建对象的时候指明保存的父位置、和子位置 */public class SaveWeb {//这是后面创建txt文件的名字String webName;//这是后面保存这个链接所有东西的的子目录和保存文本的txt文件对象,创建完对象后,就初始化完成了File myFile;File txtFile;BufferedWriter txtBw;public SaveWeb(String parentFile, String bloger) {this.webName = bloger;this.myFile = new File(parentFile,bloger);if(!this.myFile.exists()){this.myFile.mkdirs();}this.txtFile = new File(this.myFile,bloger+".txt");try {//得到这个txt文件的高效写入流,后面的方法都可以直接用txtBw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(txtFile),"utf-8"));} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void saveImg(String imgUrl) throws Exception{// 1.创建链接对象URL url = new URL(imgUrl);// 2.创建根据链接对象写出通道对象URLConnection uc = url.openConnection();uc.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0");// 3.根据通道对象,得到字节流的读的方法InputStream fis = uc.getInputStream();String imgName = imgUrl.substring(29);File file = new File(myFile, imgName);// 5.开始保存FileOutputStream fos = new FileOutputStream(file);byte[] bys = new byte[1024];int len = -1;while ((len = fis.read(bys)) != -1) {fos.write(bys, 0, len);}// 释放资源fos.close();fis.close();}public void saveWebName(String webNameIntxt) throws IOException{txtBw.newLine();txtBw.append("--------"+webNameIntxt+"-----------");txtBw.newLine();txtBw.flush();}public void saveVisit(String[] visits) throws Exception{int i = 1;txtBw.newLine();txtBw.append("---------访问量---------");txtBw.newLine();for(String visit:visits){txtBw.append(i+++"."+visit);txtBw.newLine();txtBw.flush();}}public void saveTitle(String[] titles) throws IOException{int i = 1;txtBw.newLine();txtBw.append("----------标题-----------");txtBw.newLine();for(String title:titles){txtBw.append(i+++"."+title);txtBw.newLine();txtBw.flush();}}public void saveAllHtml(String line) throws Exception{File htmlFile = new File(this.myFile,webName+".html");BufferedWriter htmlBw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile),"utf-8"));htmlBw.write(line);htmlBw.close();}public void close() throws Exception{this.txtBw.close();}}
3.下面是个测试的小例子,会把得到的信息都保存到d:\webDown下面
package cn.test12.WebRobot04;import org.junit.Test;/* * 通过现有的几个类创建得到csdn主页所有出现的博客,并把每一个博客中的所有需要的内容都下载下来 */public class GetCsdnTest02 {public static void main(String[] args) throws Exception {String[] blogHomePage = new GetCsdn("http://blog.csdn.net").getBlogHomePage();for(String blog:blogHomePage){try{function(blog);System.out.println(blog+"----下载完成");}catch(Exception e){System.out.println(blog+"----下载失败");}}}}4.
测试结果:
0 0
- 使用Java爬虫得到CSDN博客信息并保存(一)
- 使用Java爬虫得到CSDN博客信息并保存(一)
- 【初学】java爬虫并抓取图片保存
- JAVA爬虫挖取CSDN博客文章
- JAVA爬虫挖取CSDN博客文章
- 使用url读取csdn的博客访问量并将记录保存到本地
- 使用python抓取csdn博客访问量并保存在sqlite3数据库中
- 爬虫 登录csdn并获取个人博客文章列表
- java爬虫实战(1):抓取信息门户网站中的图片及其他文件并保存至本地
- Python爬虫实战一之使用Beautiful Soup抓取百度招聘信息并存储excel文件
- python 爬取csdn网页并保存博客到本地
- csdn博客爬虫更新
- 【Python脚本】-爬虫得到CSDN博客的文章访问量和评论量
- Java网络编程(一) - Java网页爬虫 - 爬取自己的CSDN博客标题和阅读数(附源码)
- Java 数据保存与读取,保存数据信息并加密
- java算法-网络爬虫抓取网页并保存
- java实现CSDN博客迁移到WordPress爬虫工具
- JAVA爬虫挖取CSDN博客文章(续)
- 命令行选择
- log4j配置
- 使用Java爬虫得到CSDN博客信息并保存(一)
- 算数-分数和小数
- Eclipse设置字体大小
- 使用Java爬虫得到CSDN博客信息并保存(一)
- python与数据库
- Python easy_install安装插件错误(UnicodeDecodeError)解决办法
- Android中ImageLoader的使用
- MySQL Fabric 实践
- 设计模式之抽象工厂模式
- Apache Kafka 分布式消息队列中间件安装与配置
- js中的一些消息提示框
- 视差特效