Java开源爬虫框架WebCollector教程——在Eclipse项目中配置使用WebCollector爬虫

在Eclipse项目中使用WebCollector爬虫非常简单,不需要任何其他的配置,只需要导入相关的jar包即可。

Netbeans、Intellij也是非常优秀的IDE,下面的方法也同样适用于Netbeans和Intellij(有细微差别),推荐使用Netbeans或Intellij。至于Netbeans和Intellij的项目结构是否通用这个问题,其实是不用考虑的,因为Eclipse项目结构也是不通用的,参与过开源软件开发的人应该知道,apache等开源组织发布的源码往往是ant项目或maven项目,这些才是通用的项目结构,并且Netbeans和Intellij在对ant和maven的支持上,比Eclipse好得多。

1.直接使用配置好的项目:

WebCollector爬取新浪微博等完整示例工程可加群250108697345054141从群文件中下载,下载群文件中任意WebCollector示例项目即可。

2.自己动手配置项目:

具体步骤如下:

1.导入依赖:

Maven:

<dependency>
    <groupId>cn.edu.hfut.dmic.webcollector</groupId>
    <artifactId>WebCollector</artifactId>
    <version>2.52</version>
</dependency>

Jar包:

如果不使用Maven,可以进入WebCollector官方网站下载最新版本所需jar包。

最新版本的jar包放在webcollector-version-bin.zip中。

2.打开Eclipse,选择File->New->Java Project,按照正常步骤新建一个JAVA项目。

在工程根目录下新建一个文件夹lib,将刚下载的webcollector-version-bin.zip解压后得到的所有jar包放到lib文件夹下。将jar包放到build path中。

3.现在可以编写WebCollector爬虫的代码了,例如我们编写一个爬取网站新闻的例子。

新建一个类NewsCrawler.java,源码如下:


import cn.edu.hfut.dmic.webcollector.model.CrawlDatums; import cn.edu.hfut.dmic.webcollector.model.Page; import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler; import org.jsoup.nodes.Document; /** * Crawling news from hfut news * * @author hu */ public class NewsCrawler extends BreadthCrawler { /** * @param crawlPath crawlPath is the path of the directory which maintains * information of this crawler * @param autoParse if autoParse is true,BreadthCrawler will auto extract * links which match regex rules from pag */ public NewsCrawler(String crawlPath, boolean autoParse) { super(crawlPath, autoParse); /*start page*/ this.addSeed("http://news.hfut.edu.cn/list-1-1.html"); /*fetch url like http://news.hfut.edu.cn/show-xxxxxxhtml*/ this.addRegex("http://news.hfut.edu.cn/show-.*html"); /*do not fetch jpg|png|gif*/ this.addRegex("-.*\\.(jpg|png|gif).*"); /*do not fetch url contains #*/ this.addRegex("-.*#.*"); } @Override public void visit(Page page, CrawlDatums next) { String url = page.getUrl(); /*if page is news page*/ if (page.matchUrl("http://news.hfut.edu.cn/show-.*html")) { /*we use jsoup to parse page*/ Document doc = page.getDoc(); /*extract title and content of news by css selector*/ String title = page.select("div[id=Article]>h2").first().text(); String content = page.select("div#artibody", 0).text(); System.out.println("URL:\n" + url); System.out.println("title:\n" + title); System.out.println("content:\n" + content); /*If you want to add urls to crawl,add them to nextLink*/ /*WebCollector automatically filters links that have been fetched before*/ /*If autoParse is true and the link you add to nextLinks does not match the regex rules,the link will also been filtered.*/ //next.add("http://xxxxxx.com"); } } public static void main(String[] args) throws Exception { NewsCrawler crawler = new NewsCrawler("crawl", true); crawler.setThreads(50); crawler.setTopN(100); //crawler.setResumable(true); /*start crawl with depth of 4*/ crawler.start(4); } }

4.运行源码即可看到输出

通过捐款支持WebCollector

维护WebCollector及教程需要花费较大的时间和精力,如果你喜欢WebCollector的话,欢迎通过捐款的方式,支持开发者的工作,非常感谢!

你可以使用支付宝钱包扫描下方的二维码进行捐款, 或者通过向支付宝帐号 [email protected]转帐进行捐款。

You may also like...

发表评论

邮箱地址不会被公开。 必填项已用*标注

推荐文章
加QQ群下载完整Eclipse项目

WebCollector网络爬虫:250108697(已满)

WebCollector网络爬虫2群:345054141

热门