https://github.com/yasserg/crawler4j

需要两个包:

  crawler4j-4.1-jar-with-dependencies.jar

  slf4j-simple-1.7.22.jar(如果不加,会有警告:SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".)

相关包下载:

http://download.csdn.net/detail/talkwah/9747407

(crawler4j-4.1-jar-with-dependencies.jar相关资料少,github下载半天还失败,故整理了一下)

参考资料:

http://blog.csdn.net/zjm131421/article/details/13093869

http://favccxx.blog.51cto.com/2890523/1691079/

import java.util.Set;
import java.util.regex.Pattern;import edu.uci.ics.crawler4j.crawler.CrawlConfig;
import edu.uci.ics.crawler4j.crawler.CrawlController;
import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.crawler.WebCrawler;
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
import edu.uci.ics.crawler4j.parser.HtmlParseData;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;
import edu.uci.ics.crawler4j.url.WebURL;public class AhCrawler extends WebCrawler {// 三要素:// _访问谁?// _怎么访?// _访上了怎么处置?private static final String C_URL = "http://www.ximalaya.com";@Overridepublic boolean shouldVisit(Page referringPage, WebURL url) {String href = url.getURL().toLowerCase();// 不匹配:MP3|jpg|png结尾的资源Pattern p = Pattern.compile(".*(\\.(MP3|jpg|png))$");return !p.matcher(href).matches() && href.startsWith(C_URL);}@Overridepublic void visit(Page page) {String url = page.getWebURL().getURL();String parentUrl = page.getWebURL().getParentUrl();String anchor = page.getWebURL().getAnchor();System.out.println("↓↓↓↓↓↓↓↓↓");System.out.println("URL        :" + url);System.out.println("Parent page:" + parentUrl);System.out.println("Anchor text:" + anchor);logger.info("URL: {}", url);logger.debug("Parent page: {}", parentUrl);logger.debug("Anchor text: {}", anchor);if (page.getParseData() instanceof HtmlParseData) {HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();String text = htmlParseData.getText();String html = htmlParseData.getHtml();Set<WebURL> links = htmlParseData.getOutgoingUrls();System.out.println("--------------------------");// System.out.println("Text length: " + text.length());// System.out.println("Html length: " + html.length());System.out.println("Number of outgoing links: " + links.size());}System.out.println("↑↑↑↑↑↑↑");}public static void main(String[] args) throws Exception {// 源代码例子中,这两位是两只参数// 配置个路径,这个路径相当于Temp文件夹,不用先建好,String crawlStorageFolder = "/data/crawl/root";int numberOfCrawlers = 7;CrawlConfig crawlConf = new CrawlConfig();crawlConf.setCrawlStorageFolder(crawlStorageFolder);PageFetcher pageFetcher = new PageFetcher(crawlConf);RobotstxtConfig robotConf = new RobotstxtConfig();RobotstxtServer robotServ = new RobotstxtServer(robotConf, pageFetcher);// 控制器CrawlController c = new CrawlController(crawlConf, pageFetcher,robotServ);// 添加网址
        c.addSeed(C_URL);// 启动爬虫c.start(AhCrawler.class, numberOfCrawlers);}
}

CrawlController c 的来历:

结果示例:

↓↓↓↓↓↓↓↓↓
URL        :http://www.ximalaya.com/5333001/sound/25320285
Parent page:http://www.ximalaya.com/dq/music-ACG/
Anchor text:俊豪演奏 - 琵琶版《刀劍如夢》
[Crawler 3] INFO edu.uci.ics.crawler4j.crawler.WebCrawler - URL: http://www.ximalaya.com/5333001/sound/25320285
--------------------------
Number of outgoing links: 131
↑↑↑↑↑↑↑
[Crawler 7] INFO edu.uci.ics.crawler4j.crawler.WebCrawler - URL: http://www.ximalaya.com/30119950/sound/12181402
↓↓↓↓↓↓↓↓↓
URL        :http://www.ximalaya.com/30119950/sound/12181402
Parent page:http://www.ximalaya.com/dq/book-果麦文化/
Anchor text:第二十六集 人生的意思不在于留下什么,而在于经历
--------------------------
Number of outgoing links: 134
↑↑↑↑↑↑↑
↓↓↓↓↓↓↓↓↓
URL        :http://www.ximalaya.com/zhubo/56833971/
Parent page:http://www.ximalaya.com/4932085/sound/21902925
Anchor text:null
[Crawler 1] INFO edu.uci.ics.crawler4j.crawler.WebCrawler - URL: http://www.ximalaya.com/zhubo/56833971/
--------------------------
Number of outgoing links: 68
↑↑↑↑↑↑↑
[Crawler 4] INFO edu.uci.ics.crawler4j.crawler.WebCrawler - URL: http://www.ximalaya.com/5413571/sound/2349697
↓↓↓↓↓↓↓↓↓
URL        :http://www.ximalaya.com/5413571/sound/2349697
Parent page:http://www.ximalaya.com/dq/renwen-新知/
Anchor text:41-方明-西江月·夜行黄沙道中 南宋 辛弃疾
--------------------------
Number of outgoing links: 134
↑↑↑↑↑↑↑
[Crawler 6] INFO edu.uci.ics.crawler4j.crawler.WebCrawler - URL: http://www.ximalaya.com/5011186/sound/30650945
↓↓↓↓↓↓↓↓↓
URL        :http://www.ximalaya.com/5011186/sound/30650945
Parent page:http://www.ximalaya.com/dq/finance-大咖/
Anchor text:03
--------------------------
Number of outgoing links: 111
↑↑↑↑↑↑↑
↓↓↓↓↓↓↓↓↓
URL        :http://www.ximalaya.com/1000144/album/3559805
Parent page:http://www.ximalaya.com/dq/music-文艺/
Anchor text:null
[Crawler 2] INFO edu.uci.ics.crawler4j.crawler.WebCrawler - URL: http://www.ximalaya.com/1000144/album/3559805
--------------------------
Number of outgoing links: 85
↑↑↑↑↑↑↑
↓↓↓↓↓↓↓↓↓
URL        :http://www.ximalaya.com/4932085/sound/21902925/liker
Parent page:http://www.ximalaya.com/4932085/sound/21902925
Anchor text:更多
[Crawler 1] INFO edu.uci.ics.crawler4j.crawler.WebCrawler - URL: http://www.ximalaya.com/4932085/sound/21902925/liker
--------------------------
Number of outgoing links: 96
↑↑↑↑↑↑↑
↓↓↓↓↓↓↓↓↓
URL        :http://www.ximalaya.com/30895669/sound/19945445
Parent page:http://www.ximalaya.com/dq/music-ACG/
Anchor text:宫崎骏-久石让
[Crawler 3] INFO edu.uci.ics.crawler4j.crawler.WebCrawler - URL: http://www.ximalaya.com/30895669/sound/19945445
--------------------------
Number of outgoing links: 131
↑↑↑↑↑↑↑
↓↓↓↓↓↓↓↓↓
URL        :http://www.ximalaya.com/9112346/album/2903291
Parent page:http://www.ximalaya.com/dq/book-果麦文化/
Anchor text:null
[Crawler 7] INFO edu.uci.ics.crawler4j.crawler.WebCrawler - URL: http://www.ximalaya.com/9112346/album/2903291
--------------------------
Number of outgoing links: 90
↑↑↑↑↑↑↑

转载于:https://www.cnblogs.com/AndyHoo/p/6367622.html

网络爬虫2:使用crawler4j爬取网络内容相关推荐

  1. Java网络爬虫(三)爬取网络小说

    因CSDN版权问题,小说网站的URL.图片不可在此公布,读者根据自己想要爬取的网站,自行选择网站即可. 1.爬取小说章节内容,需要注意的大部分原创小说内容页是禁用右键的,无法直接选取页面内容进行元素检 ...

  2. iOS开发——网络使用技术OC篇网络爬虫-使用正则表达式抓取网络数据

    网络爬虫-使用正则表达式抓取网络数据 关于网络数据抓取不仅仅在iOS开发中有,其他开发中也有,也叫网络爬虫,大致分为两种方式实现 1:正则表达 2:利用其他语言的工具包:java/Python 先来看 ...

  3. 数据采集与存储案例——基于Python爬虫框架Scrapy的爬取网络数据与MySQL数据持久化

    此案例需要预先安装pymsql python3.7.4 scrapy2.7.1 一.安装scrapy框架 1.使用pip命令安装scrapy pip install scrapy 在这里下载太慢可以使 ...

  4. Python网络爬虫requests、bs4爬取空姐图片,福利哦

    Scrapy框架很好,也提供了很多扩展点,可以自己编写中间件处理Scrapy的Request和Response.但是可定制化或者可掌控性来说,还是自己写的爬虫更加强一些. 接下来,我们来看一下使用Py ...

  5. python网络爬虫实战——利用逆向工程爬取动态网页

    前言 之前写过介绍过网络爬虫的文章,都是一些基础知识,所以在接下来我们完成一个小任务--利用逆向工程爬取一个动态网页,360壁纸网站(https://image.so.com/z?ch=wallpap ...

  6. Python网络爬虫:利用正则表达式爬取豆瓣电影top250排行前10页电影信息

    在学习了几个常用的爬取包方法后,转入爬取实战. 爬取豆瓣电影早已是练习爬取的常用方式了,网上各种代码也已经很多了,我可能现在还在做这个都太土了,不过没事,毕竟我也才刚入门-- 这次我还是利用正则表达式 ...

  7. 网络爬虫(动态网页爬取关于混合动力汽车的论文数据)

    一.实验目的 (1) 熟悉网页浏览器开发工具的使用: (2) 掌握动态网页加载过程: (3) 掌握 post 请求参数设置: (4) 掌握基本的反反爬技术: (5) 能够根据问题需求,指定网络爬虫方案 ...

  8. 网络爬虫:使用多线程爬取网页链接

    前言: 经过前面两篇文章,你想大家应该已经知道网络爬虫是怎么一回事了.这篇文章会在之前做过的事情上做一些改进,以及说明之前的做法的不足之处. 思路分析: 1.逻辑结构图 上图中展示的就是我们网络爬虫中 ...

  9. 网络爬虫---用urllib模块爬取京东笔记本电脑的数据、并对其做一个可视化

    用urllib模块爬取京东笔记本电脑的数据.并对其做一个可视化 文章目录 用urllib模块爬取京东笔记本电脑的数据.并对其做一个可视化 一.前言 二.知识要求 三.过程分析 1.观察主页面和每个电脑 ...

  10. Python网络爬虫(6)--爬取淘宝模特图片

    经过前面的一些基础学习,我们大致知道了如何爬取并解析一个网页中的信息,这里我们来做一个更有意思的事情,爬取MM图片并保存.网址为https://mm.taobao.com/json/request_t ...

最新文章

  1. 游戏设计行业标准测试:秘密收集
  2. Apache转发到Tomcat
  3. 十个鲜为人知的Linux命令 - Part 5
  4. python 井字游戏
  5. Sublime text 简单配置
  6. c语言查找字符串au,几个C语言词汇不懂,望老鸟们相助(俺是新手哦)
  7. php 清空一个数组_php如何删除一个数组
  8. 9:14 2009-7-22
  9. Linux: wget 使用技巧
  10. 求python一个类与对象的代码_Python基础系列(五)类和对象,让你更懂你的python代码...
  11. 21天jmeter打卡day5-完成对www.baidu.com的两条接口测试
  12. 向模块化进军,创建类
  13. form 表单添加请求头_Golang GinWeb框架5-绑定多种请求类型的数据
  14. 计算机二级操作题微盘,全国计算机等级考试题及答案二.pdf
  15. Mac下Zookeeper安装及配置
  16. java生成xlsx文件
  17. VMware Workstation -- 破解密码
  18. cc域名转入可以吗?其特点是什么?
  19. 如何解决Java查看源代码时页面显示Source not found
  20. NI multism 设计单脉冲发生器实验报告

热门文章

  1. Nexus5548UP系统升级
  2. 开源框架到CocoaPods
  3. 电池的寿命(贪心算法)
  4. 揭秘为什么企业都自己开发直播APP
  5. 【生信】R语言在RNA-seq中的应用
  6. 与 UltraEdit/UEStudio 共享 FTP 帐户
  7. 企业微信小程序_集成腾讯地图实现精准定位考勤打卡
  8. 热门软件看点:IE和它的对手们
  9. Amazon EMR 配置 Hive 对Hue用户的权限控制
  10. 多极磁环测试、表磁分布测试、磁场波形测试设备介绍