案例2:从网上获取《三国演义》全文
需求说明
搭建开发环境,实现《三国演义》全文保存在本地
步骤分析
- 访问网址:http://www.shicimingju.com/book/sanguoyanyi.html
- 分析网站URL、文档内容特征
- 获取网页内容
- 拆分出需求内容
- 保存在本地 D:\三国演义.txt
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NovelDemo {
/**
* 1、根据小说存放位置创建file对象
* 2、根据网页结构编写正则,创建pattern对象
* 3、编写循环,创建向所有小说章节页面发起网络请求的url对象
* 4、网络流BufferReader
* 5、创建输入流
* 6、循环读取请求得到的内容,使用正则匹配其中的内容
* 7、将读取到的内容写入本地文件,知道循环结束
* 8、注意代码中的异常处理
*/
public static void main(String[] args) {
// 1、根据小说存放位置创建file对象
File file = new File("D:\\三国演义.txt");
if(!file.exists()){ //文件不存在则创建该文件
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// 2、根据网页结构编写正则,创建pattern对象
String regex_content = "<p.*?>(.*?)</p>";
String regex_title = "<h1>(.*?)</h1>";
Pattern p_content = Pattern.compile(regex_content);
Pattern p_title = Pattern.compile(regex_title);
Matcher m_content;
Matcher m_title;
// 3、编写循环,创建向所有小说章节页面发起网络请求的url对象
for (int i = 1; i <= 120; i++) {
System.out.println("第" + i + "章开始下载。。。");
try {
// 创建每一个页面的url对象
URL url = new URL("http://www.shicimingju.com/book/sanguoyanyi/" + i + ".html");
// 创建网络读取流
BufferedReader reader = new BufferedReader(
new InputStreamReader(url.openStream(), "utf8"));
// 4、读取网络内容网络流BufferReader
String str = null;
// 5、创建输入流
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file, true)));
while ((str = reader.readLine()) != null) {
m_title = p_title.matcher(str.toString());
m_content = p_content.matcher(str.toString());
// 获取小说标题并写入本地文件
Boolean isEx = m_title.find();
if (isEx) {
String title = m_title.group();
// 清洗得到的数据
title = title.replace("<h1>", "").replace( "</h1>", "");
System.out.println(title);
writer.write("第" + i + "章:" + title + "\n");
}
while (m_content.find()) {
String content = m_content.group();
// 清洗得到的数据
content = content.replace("<p>", "")
.replace("</p>", "")
.replace(" ", "")
.replace("?", "");
// 把小说内容写入文件
writer.write(content + "\n");
}
}
System.out.println("第" + i + "章下载完成.........");
writer.write("\n\n");
writer.close();
reader.close();
} catch (Exception e) {
System.out.println("下载失败");
e.printStackTrace();
}
}
}
}
超全面的测试IT技术课程,0元立即加入学习!有需要的朋友戳:
腾讯课堂测试技术学习地址
欢迎转载,但未经作者同意请保留此段声明,并在文章页面明显位置给出原文链接。