




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
看了htmlparser工具包发现果然强大。由于不是很熟悉 所以下面代码或许写的有点烂。首先做准备工作 先写一个实体beanpackage bean; import java.util.Date;/* author Jeson* blog * date:Oct 9, 2009 3:09:19 PM* version :1.0*/public class Artical private String title; private String body; private String link; private String author; private String tags; private Date dCreate; public String getTitle() return title; public void setTitle(String title) this.title = title; public String getBody() return body; public void setBody(String body) this.body = body; public String getLink() return link; public void setLink(String link) this.link = link; public String getAuthor() return author; public void setAuthor(String author) this.author = author; public String getTags() return tags; public void setTags(String tags) this.tags = tags; public Date getDCreate() return dCreate; public void setDCreate(Date create) dCreate = create; 2 写一个我们下面要用到的字符串处理类package util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.regex.Matcher;import java.util.regex.Pattern;/* author Jeson* blog * date:Oct 9, 2009 3:09:19 PM * version :1.0*/public class StringUtil /* * 使用正则匹配字符串 * * param regex * 正则表达式 * param txt * 要验证的字符串 * return 匹配则返回真 否则返回假 */ public static boolean useRegex(String regex, String txt) Pattern p = Ppile(regex); Matcher m = p.matcher(txt); return m.matches(); /* * 使用正则匹配字符串 * * param regex * 正则表达式 * * param index * 要取第几个元素 * param txt * 要验证的字符串 * return 返回匹配的字符串 */ public static String getByRegex(String regex, int index, String txt) Pattern p = Ppile(regex); Matcher m = p.matcher(txt); if (m.find() return m.group(index); return null; /* * 使用正则匹配字符串 * * param regex * 正则表达式 * * param index * 要取第几个元素 * param txt * 要验证的字符串 * return 返回匹配的字符串数组 */ public static String getStringsByRegex(String regex, int index, String txt) String res = new Stringindex.length; Pattern p = Ppile(regex); Matcher m = p.matcher(txt); if (m.find() for(int i : index) resi = m.group(i); return res; 3 下面是我们的核心类 他会去抓取cnblogs的页面并保存 package test; import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import org.htmlparser.NodeFilter;import org.htmlparser.Parser;import org.htmlparser.filters.HasAttributeFilter;import org.htmlparser.util.NodeList;import bean.Artical;import util.StringUtil;/* author Jeson* blog * date:Oct 9, 2009 1:08:10 PM* version :1.0*/public class Parse private static final int MAX_PAGE = 20; private final String ENCODING = UTF-8; /* * param args */ public static void main(String args) try for(int i=1;iMAX_PAGE;i+) new Parse().testAttribute(i); catch (Exception e) / TODO Auto-generated catch block e.printStackTrace(); private void testAttribute(int pa) throws Exception System.out.println(开始解析页面:+pa); Parser p = new Parser(); p.setURL(/cate/java/?page=+pa); p.setEncoding(UTF-8); NodeFilter filter = new HasAttributeFilter(class,titlelnk); NodeList list = p.extractAllNodesThatMatch(filter); System.out.println(list.size(); int cou = 0; for(int i=0 ; ilist.size();i+) String html = list.elementAt(i).toHtml(true); int index = 0,1,2; String bs = StringUtil.getStringsByRegex(.*), index, html); String title = bs2; String url = bs1; System.out.println(url); String content = getContent(bs1); if(content = null | .equals(content) continue; Artical art = new Artical(); art.setTitle(title); art.setBody(content); art.setLink(url); createFile(art); System.out.println(=+(i+1)+=); System.out.println(title=+bs2); System.out.println(url=+bs1); System.out.println(content=+getContent(bs1); System.out.println(=); System.out.println(); cou+; System.out.println(over+cou); private String getContent(String url) throws Exception Parser p = new Parser(); p.setURL(url); p.setEncoding(ENCODING); NodeFilter filter = new HasAttributeFilter(class,post); NodeList list = p.extractAllNodesThatMatch(filter); String a = list.toHtml(); return a; private void createFile(Artical art) try File d = new File(d:cnblog); if(!d.exists() d.mkdir(); File f = new File(d:cnblog+art.getTitle()+.html); if(!f.exists() f.createNewFile(); System.out.println(+art.getTitle()+文件已经创建); OutputStream file = new FileOutputStream(f.getPath(); file.write(art.getBody().getBy
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年初中英语中考试题及答案
- 大唐电力泸州市2025秋招面试典型题目及答案
- 吴忠市中石油2025秋招笔试模拟题含答案财务与审计岗
- 中国移动红河自治州2025秋招笔试行测题库及答案财务审计类
- 中国广电成都市2025秋招市场与服务类专业追问清单及参考回答
- 海口市中储粮2025秋招财务资产岗高频笔试题库含答案
- 中国移动桂林市2025秋招技能类专业追问清单及参考回答
- 安阳市中石油2025秋招笔试模拟题含答案炼油设备技术岗
- 中国广电保定市2025秋招笔试行测题库及答案互联网运营
- 中国广电广安市2025秋招笔试行测题库及答案市场与服务类
- GB/T 11376-2020金属及其他无机覆盖层金属的磷化膜
- 2022年《体育法》全文PPT
- 公司区域经营管理办法
- 五年级《中国民间故事》知识考试题库(含答案)
- 《景观生态设计》课件
- 江苏省南通市各县区乡镇行政村村庄村名居民村民委员会明细
- 中国古典乐器-古筝琵琶英文介绍(带翻译)课件
- 地表形态塑造内力作用说课
- QSY02553-2018井下作业井控技术规范
- 苏教版(新教材)三年级上册小学科学第二单元测试卷含答案
- 音乐 认识音乐课件
评论
0/150
提交评论