1、接口
/** *报纸采集器 * @author 杨尚川 */ public interface PaperCollector { /** * 下载当日报纸,一个文件对应一个版面 * @return 报纸 */ List<File> collect(); /** * 下载指定日期的报纸,一个文件对应一个版面 * @param date 指定日期 * @return 报纸 */ List<File> collect(Date date); }
2、抽象类
/** *报纸采集器抽象类,通用采集功能实现 * @author 杨尚川 */ public abstract class AbstractPaperCollector implements PaperCollector{ protected final Logger LOG = LoggerFactory.getLogger(getClass()); @Override public List<File> collect() { return collect(new Date()); } /** * 根据下载链接提取文件夹名称 * @param href 下载链接 * @return 文件夹名称 */ protected abstract String getPath(String href); /** * 根据下载链接提取文件名称 * @param href 下载链接 * @return 文件名称 */ protected abstract String getFile(String href); protected List<File> downloadPaper(List<String> hrefs){ final List<File> files = new ArrayList<>(); List<Thread> ts = new ArrayList<>(); LOG.info("报纸有"+hrefs.size()+"个版面需要下载:"); for(final String href : hrefs){ Thread t = new Thread(new Runnable(){ @Override public void run() { File file = downloadPaper(href); if(file != null){ files.add(file); } } }); t.start(); ts.add(t); } for(Thread t : ts){ try { t.join(); } catch (InterruptedException ex) { LOG.error("下载报纸出错:",ex); } } return files; } protected File downloadPaper(String href){ try{ LOG.info("下载报纸:"+href); String path = getPath(href); LOG.debug("报纸保存目录:"+path); String file = getFile(href); LOG.debug("报纸保存文件:"+file); File dir = new File(path); if(!dir.exists()){ LOG.debug("创建目录:"+dir.getAbsolutePath()); dir.mkdirs(); } File absoluteFile = new File(path, file); LOG.debug("报纸保存绝对路径:"+absoluteFile.getAbsolutePath()); Tools.copyFile(new URL(href).openStream(), absoluteFile); LOG.info("报纸下载成功:"+href); LOG.info("报纸成功保存到:"+absoluteFile.getAbsolutePath()); return absoluteFile; }catch(IOException e){ LOG.error("报纸下载失败:"+e); } return null; } protected void run() { //今天 List<File> files = collect(); int i = 1; for(File file : files){ LOG.info((i++)+" : " + file.getAbsolutePath()); } //昨天 Date date = new Date(); date.setTime(System.currentTimeMillis()-24*3600*1000); files = collect(date); i = 1; for(File file : files){ LOG.info((i++)+" : " + file.getAbsolutePath()); } //前天 date = new Date(); date.setTime(System.currentTimeMillis()-2*24*3600*1000); files = collect(date); i = 1; for(File file : files){ LOG.info((i++)+" : " + file.getAbsolutePath()); } } }
3、采集新华日报
/** * 新华日报 * @author 杨尚川 */ public class XHRBPaperCollector extends AbstractPaperCollector{ private static final String paperName = "新华日报"; private static final String paperPath = "http://xh.xhby.net/newxh/"; private static final String url = paperPath+"html/"; private static final String hrefPrefix = paperPath+"page/1/"; private static final String start = "node_2.htm"; private static final String pdfCssQuery = "html body table tbody tr td table tbody tr td table tbody tr td table tbody tr td div table tbody tr td a"; private static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM/dd/"); @Override public List<File> collect(Date date) { List<String> hrefs = new ArrayList<>(); try { LOG.debug("url: "+url); String paper = url + sf.format(date) + start; LOG.debug("paper: "+paper); Document document = Jsoup.connect(paper).get(); LOG.debug("pdfCssQuery: " + pdfCssQuery); Elements elements = document.select(pdfCssQuery); for(Element element : elements){ String href = element.attr("href"); if(href != null && href.endsWith(".pdf")){ LOG.debug("报纸链接:"+href); href = href.replace("../../../", ""); LOG.debug("报纸链接:"+href); hrefs.add(paperPath+href); }else{ LOG.debug("不是报纸链接:"+href); } } } catch (IOException ex) { LOG.error("采集出错",ex); } return downloadPaper(hrefs); } @Override protected String getPath(String href) { String path = href.replace(hrefPrefix, ""); String[] attrs = path.split("/"); attrs = attrs[0].split("-"); StringBuilder str = new StringBuilder(); str.append(paperName) .append(File.separator) .append(attrs[0]) .append("-") .append(attrs[1]) .append(File.separator) .append(attrs[2]); return str.toString(); } @Override protected String getFile(String href) { String path = href.replace(hrefPrefix, ""); String[] attrs = path.split("/"); String file = attrs[1]+".pdf"; return file; } public static void main(String[] args) { new XHRBPaperCollector().run(); } }
4、采集楚天都市报
/** * 楚天都市报 * @author 杨尚川 */ public class CTDSBPaperCollector extends AbstractPaperCollector{ private static final String paperName = "楚天都市报"; private static final String host = "http://ctdsb.cnhubei.com/"; private static final String paperPath = host+"ctdsb/"; private static final String url = host+"html/ctdsb/"; private static final String hrefPrefix = paperPath; private static final String start = "index.html"; private static final String pdfCssQuery = "html body center table tbody tr td table tbody tr td table tbody tr td table tbody tr td div table tbody tr td.info3 a"; private static final SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd/"); @Override public List<File> collect(Date date) { List<String> hrefs = new ArrayList<>(); try { LOG.debug("url: "+url); String paper = url + sf.format(date) + start; LOG.debug("paper: "+paper); Document document = Jsoup.connect(paper).get(); LOG.debug("pdfCssQuery: " + pdfCssQuery); Elements elements = document.select(pdfCssQuery); int count=0; for(Element element : elements){ String text = element.text(); if(text != null && text.startsWith("第")){ LOG.debug("报纸文本:"+text); count++; }else{ LOG.debug("不是报纸文本:"+text); } } //有的版面缺失,而文件名是顺序递增的 for(int i=1; i<=count; i++){ String seq = Integer.toString(i); if(i<10){ seq="0"+seq; } hrefs.add(paperPath + sf.format(date) + "page_"+seq+".jpg"); } } catch (IOException ex) { LOG.error("采集出错",ex); } return downloadPaper(hrefs); } @Override protected String getPath(String href) { String path = href.replace(hrefPrefix, ""); String[] attrs = path.split("/"); StringBuilder str = new StringBuilder(); str.append(paperName) .append(File.separator) .append(attrs[0].substring(0, 4)) .append("-") .append(attrs[0].substring(4, 6)) .append(File.separator) .append(attrs[0].substring(6, 8)); return str.toString(); } @Override protected String getFile(String href) { String path = href.replace(hrefPrefix, ""); String[] attrs = path.split("/"); String file = attrs[1].split("_")[1]; return file; } public static void main(String[] args) { new CTDSBPaperCollector().run(); } }
5、采集京九晚报
/** * 京九晚报 * @author 杨尚川 */ public class JJWBPaperCollector extends AbstractPaperCollector{ private static final String paperName = "京九晚报"; private static final String paperPath = "http://epaper.cnsq.com.cn/jjwb/"; private static final String url = paperPath+"html/"; private static final String hrefPrefix = paperPath+"page/10/"; private static final String start = "node_11.htm"; private static final String pdfCssQuery = "html body table tbody tr td table tbody tr td table tbody tr td table tbody tr td div table tbody tr td a"; private static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM/dd/"); @Override public List<File> collect(Date date) { List<String> hrefs = new ArrayList<>(); try { LOG.debug("url: "+url); String paper = url + sf.format(date) + start; LOG.debug("paper: "+paper); Document document = Jsoup.connect(paper).get(); LOG.debug("pdfCssQuery: " + pdfCssQuery); Elements elements = document.select(pdfCssQuery); for(Element element : elements){ String href = element.attr("href"); if(href != null && href.endsWith(".pdf")){ LOG.debug("报纸链接:"+href); href = href.replace("../../../", ""); LOG.debug("报纸链接:"+href); hrefs.add(paperPath+href); }else{ LOG.debug("不是报纸链接:"+href); } } } catch (IOException ex) { LOG.error("采集出错",ex); } return downloadPaper(hrefs); } @Override protected String getPath(String href) { String path = href.replace(hrefPrefix, ""); String[] attrs = path.split("/"); StringBuilder str = new StringBuilder(); str.append(paperName) .append(File.separator) .append(attrs[0]) .append(File.separator) .append(attrs[1]); return str.toString(); } @Override protected String getFile(String href) { String path = href.replace(hrefPrefix, ""); String[] attrs = path.split("/"); String file = attrs[2]+".pdf"; return file; } public static void main(String[] args) { new JJWBPaperCollector().run(); } }
6、采集信息时报
/** * 信息时报 * @author 杨尚川 */ public class XXSBPaperCollector extends AbstractPaperCollector{ private static final String paperName = "信息时报"; private static final String host = "http://informationtimes.dayoo.com/"; private static final String paperPath = host+"page/1019/"; private static final String url = host+"html/"; private static final String hrefPrefix = paperPath; private static final String start = "node_1019.htm"; private static final String pdfCssQuery = "html body#content div.container div.leftcolumn div.leftcolumncontent div.pagebuttontwo div.con p.right span.dfive a"; private static final String subCssQuery = "html body#listcontent div.container div.rightcolumn div.subcbga div.listcontent div#all_article_list.list h4 span.left a"; private static final String contentCssQuery = "html body div.container div.leftcolumn div.tbga div.bbga div.cbga div.left div.pagepicture div map area"; private static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM/dd/"); @Override public List<File> collect(Date date) { List<String> hrefs = new ArrayList<>(); try { LOG.debug("url: "+url); String paper = url + sf.format(date) + start; LOG.debug("paper: "+paper); Document document = Jsoup.connect(paper).get(); //1、找到子报纸 LOG.debug("subCssQuery: " + subCssQuery); Elements elements = document.select(subCssQuery); for(Element element : elements){ String text = element.text(); String href = element.attr("href"); if(text != null && text.contains(":") && href != null && href.endsWith(".htm")){ String subPaperURL = url + sf.format(date) + href; LOG.debug("子报纸文本:"+text+" , "+href); LOG.debug("subPaperURL:"+subPaperURL); //2、找到内容页面 LOG.debug("contentCssQuery: " + contentCssQuery); Elements contentElements = Jsoup.connect(subPaperURL).get().select(contentCssQuery); for(Element contentElement : contentElements){ String h = contentElement.attr("href"); if(h != null && h.startsWith("content_") && h.endsWith(".htm")){ String contentURL = url + sf.format(date) + h; LOG.debug("contentURL:"+contentURL); //3、找PDF LOG.debug("pdfCssQuery: " + pdfCssQuery); Elements pdfElements = Jsoup.connect(contentURL).get().select(pdfCssQuery); for(Element pdfElement : pdfElements){ String pdf = pdfElement.attr("href"); if(pdf != null && pdf.endsWith(".pdf")){ LOG.debug("报纸链接:"+pdf); pdf = pdf.replace("../../../", ""); LOG.debug("报纸链接:"+pdf); hrefs.add(host+pdf); }else{ LOG.debug("不是报纸链接:"+pdf); } } //有多个content,选择一个即可 break; } } }else{ LOG.debug("不是子报纸文本:"+text+" , "+href); } } } catch (IOException ex) { LOG.error("采集出错",ex); } return downloadPaper(hrefs); } @Override protected String getPath(String href) { String path = href.replace(hrefPrefix, ""); String[] attrs = path.split("/"); StringBuilder str = new StringBuilder(); str.append(paperName) .append(File.separator) .append(attrs[0]) .append(File.separator) .append(attrs[1]); return str.toString(); } @Override protected String getFile(String href) { String path = href.replace(hrefPrefix, ""); String[] attrs = path.split("/"); String file = attrs[2]+".pdf"; return file; } public static void main(String[] args) { new XXSBPaperCollector().run(); } }
7、采集羊城晚报
/** * 羊城晚报 * @author 杨尚川 */ public class YCWBPaperCollector extends AbstractPaperCollector{ private static final String paperName = "羊城晚报"; private static final String paperPath = "http://www.ycwb.com/ePaper/ycwb/"; private static final String url = paperPath+"html/"; private static final String hrefPrefix = paperPath+"images/"; private static final String start = "node_2081.htm"; private static final String pdfCssQuery = "html body div.cbody div.areaL div.box div.conBox2 div div.xx h2 em a.px12"; private static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM/dd/"); @Override public List<File> collect(Date date) { List<String> hrefs = new ArrayList<>(); try { LOG.debug("url: "+url); String paper = url + sf.format(date) + start; LOG.debug("paper: "+paper); Document document = Jsoup.connect(paper).get(); LOG.debug("pdfCssQuery: " + pdfCssQuery); Elements elements = document.select(pdfCssQuery); for(Element element : elements){ String href = element.attr("href"); if(href != null && href.endsWith(".pdf")){ LOG.debug("报纸链接:"+href); href = href.replace("../../../", ""); LOG.debug("报纸链接:"+href); hrefs.add(paperPath+href); }else{ LOG.debug("不是报纸链接:"+href); } } } catch (IOException ex) { LOG.error("采集出错",ex); } return downloadPaper(hrefs); } @Override protected String getPath(String href) { String path = href.replace(hrefPrefix, ""); String[] attrs = path.split("/"); StringBuilder str = new StringBuilder(); str.append(paperName) .append(File.separator) .append(attrs[0]) .append(File.separator) .append(attrs[1]); return str.toString(); } @Override protected String getFile(String href) { String path = href.replace(hrefPrefix, ""); String[] attrs = path.split("/"); String file = attrs[2]+".pdf"; return file; } public static void main(String[] args) { new YCWBPaperCollector().run(); } }
相关推荐
采集地方报刊电子版本的爬虫
应用领域:1、采集人物信息2、采集电子报纸3、使用NekoHTML和XPath解析百度搜索返回结果4、使用JSoup和CSSPath解析百度搜索返回结果5、使用Google AJAX API获取谷歌搜索结果 标签:search
点击一个你想要查看的报纸,是点击旁边的超链接而不是图片,然后软件就会自动联网下载该报纸的电子板,稍等片刻就可以开始阅读了。有个明了的下载进度条方便你查看该报纸的下载完成度。 最值得一提的是在8点后就可以...
php和mysql开发的报刊订阅管理系统 毕业设计整套列表:(80)ASP+ACCESS企业公司网站 ASP.NET+SQL2000销售管理系统 ASP+ACCESS网上考试系统 ASP.NET+ACCESS校友录毕业设计 ASP+ACCESS在线考试系统 ASP.NET+SQL2000...
皮尔逊卡方检验的P值0.034的统计计算值(小于5%)表明,通过订阅定价,降低了电子纸成本的积极影响,以及这最终如何影响印刷媒体的定价,销售和收入采集。 结果还显示,随着Internet成本的持续下降,通过订阅可以...
( ) 4、视频采集是将视频信号数字化并记录到文件上的过程。( ) 5、一个完整的多媒体系统由多媒体硬件和多媒体软件两部分构成。( ) 6、OCR软件的功能是将矢量化的文字转换为电子文档可识别的文字。( ) 7、...
( ) 4、视频采集是将视频信号数字化并记录到文件上的过程。( ) 5、一个完整的多媒体系统由多媒体硬件和多媒体软件两部分构成。( ) 6、OCR软件的功能是将矢量化的文字转换为电子文档可识别的文字。( ) 7、...
美国匹兹堡大学公共卫生学院将记录在报纸、报告、微缩胶片上美国各地自1888年以来有关传染病发生和死亡的多元、碎片、海量的数据收集、整理并数码化。通过数据建模和分析,把一百多年的历史"死"数据变活,建立了1888...
使用本工具可极大地提高数据处理的效率,对数据库提供单位、情报搜集部门、企业资料部门、出版社、报刊杂志社、电子图书制作单位、各行业网站和信息中心等有广泛的推广使用价值。 本工具有以下特点: 1、具有批量...
不关注数据采集的方式和方法 4 只用定量数据,没有定性数据 5 设计中的数据分析 5 数据——判断淘宝店铺页面设计优劣的显微镜 7 第2章 交互设计 11 交互到底是做什么的 12 好习惯和坏习惯 13 交互设计师容易犯的...
信息处理是计算机应用最广泛的领域,包括办公自动化(OA)、企业管理、 情报检索、报刊编排处理等,处理的信息有文字、图形、声音、图像等各种信息。计算 机可以用于科学技术、军事、工业、农业等各个领域的过程控制...
数据传统途径也发生了很大的变革,以前获取信息的来源基本上是报纸等平面媒体,或者电视、广播等传播媒体;现在很多信息来源通过互联网。互联网已经变成了媒体传播的主要途径,这个改变对整个社会也产生了非常大的...
消防的信息化建设是利用先进可靠、实用有效的现代计算机、 网络及通信技术对消防信息进行采集、储存、处理、分析和挖掘,以实现消防信息资源 和基础设施高程度、高效率、高效益的共享与共用的过程。它的范畴涵盖了...