java实现自动下载免费小说
来源:互联网 发布:吉林省网络发票系统 编辑:程序博客网 时间:2024/04/29 10:15
在百度上看到别人要下一些小说,就想自己写个下小说的玩玩。
以下为自己玩玩的,不必当真。
package edu.uci.ics.crawler4j.test;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;/** * * @author andy.wang * @时间 2017年5月27日 */public class Plzl {public static final int READ_SIZE = 8 * 1024;private static String path="F://three//";private static String domainName="http://www.58xs.com";public static void main(String[] args) throws Exception {String s=domainName+"/html/122/122467/6226722.html";Plzl p = new Plzl();p.requestByGetMethod(s);for (int i = 0; i < 151; i++) {System.out.println("info:"+s);String u=p.requestByGetMethod(s);if ("".equals(u)) {//如果没拿到下一章的就再拿一次u=p.requestByGetMethod(s);if ("".equals(u)) {System.out.println("error:"+s);//如果还是出错就报错出来}}if (!"".equals(u)) {s=u.trim();}Thread.sleep(2000);}} private CloseableHttpClient getHttpClient(){ return HttpClients.createDefault(); } private void closeHttpClient(CloseableHttpClient client) throws IOException{ if (client != null){ client.close(); } }/** * 通过GET方式发起http请求 */public String requestByGetMethod(String url){String u="";//创建默认的httpClient实例CloseableHttpClient httpClient = getHttpClient();try {//用get方法发送http请求HttpGet get = new HttpGet(url);CloseableHttpResponse httpResponse = null;//发送get请求httpResponse = httpClient.execute(get);try{//response实体HttpEntity entity = httpResponse.getEntity();if (null != entity){String s =EntityUtils.toString(entity,"utf-8");u= read(s);}}finally{httpResponse.close();}} catch (Exception e) {e.printStackTrace();}finally{try{closeHttpClient(httpClient);} catch (IOException e){e.printStackTrace();}}return u;}private String read(String s) throws IOException{String u="";String txt =path+"//pmzlx.txt";// 创建文件输出流,第二个参数表示为追加的方式输出OutputStream os = new FileOutputStream(txt, true);// 转换为字符输出流,并指定文件编码为utf-8OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");// 以自定编码的方式读取输入流if (s.lastIndexOf("content")>0) {String tilte = s.substring(s.indexOf("<h1>")+4,s.indexOf("</h1>"));//获取章节名称osw.write(tilte+"\r\n");s =s.substring(s.indexOf("下一章"),s.lastIndexOf("下一章"));//截取内容u = s.substring(s.length()-31, s.length()-2);//获取下一章的urls=s.substring(s.lastIndexOf("content")+9,s.length());//截取正文s=s.substring(0,s.indexOf("上一章"));//截取正文s=delHTMLTag(s);//去除html并且osw.write(s.replaceAll(" ", "\r\n")+"\r\n");//空格替换为换行然后写到txt文本里osw.flush();}osw.close();os.close();if ("".equals(u)) {return u;}return domainName+u;} public String delHTMLTag(String htmlStr){ String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式 String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式 String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式 Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); Matcher m_script=p_script.matcher(htmlStr); htmlStr=m_script.replaceAll(""); //过滤script标签 Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); Matcher m_style=p_style.matcher(htmlStr); htmlStr=m_style.replaceAll(""); //过滤style标签 Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); Matcher m_html=p_html.matcher(htmlStr); htmlStr=m_html.replaceAll(""); //过滤html标签 return htmlStr.trim(); //返回文本字符串 } }
阅读全文
0 0
- java实现自动下载免费小说
- 免费小说下载 大周皇族 皇甫奇
- java实现文件下载功能,自动弹出保存窗口
- java实现文件下载功能,自动弹出保存窗口
- java实现文件自动打包成zip并下载…
- expect实现ftp自动下载
- js实现自动下载功能
- 浩晴阅读免费小说
- java 实现文件下载的时候,避免浏览器自动打开文件,而是出现另存为对话框
- Java实现文件自动打包成zip并下载的代码
- Java实现文件自动打包成zip并下载的代码
- java实现51cto网站的自动签到与获取下载积分等功能代码
- Java spring中实现自动生成Excel文件并可下载
- Java 实现自动登陆
- Java实现Http下载
- java实现文件下载
- java 实现下载功能
- java实现资源下载
- Bootstrap3.0学习第十三轮(导航条)
- Python中文分词 jieba 十五分钟入门与进阶
- Kafka之第三课
- 多线程断点续传要点
- C及C++数据类型
- java实现自动下载免费小说
- 20170527:初学java spring的困惑
- 我的博客
- 170527 逆向-CrackMe(4)
- 析构函数
- HDU5172-GTY's gay friends
- Android系统中Parcelable和Serializable处理
- 网页(H5/JS)游戏
- Spring_SSH整合之_架构的历史(架构整合JSP+Hibernate+Struts2)_5