1 Star 0 Fork 1.9K

JimSow / webmagic

forked from 黄亿华 / webmagic 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

logo

Build Status

官方网站http://webmagic.io/

webmagic是一个开源的Java垂直爬虫框架,目标是简化爬虫的开发流程,让开发者专注于逻辑功能的开发。webmagic的核心非常简单,但是覆盖爬虫的整个流程,也是很好的学习爬虫开发的材料。作者曾经在前公司进行过一年的垂直爬虫的开发,webmagic就是为了解决爬虫开发的一些重复劳动而产生的框架。

web爬虫是一种技术,webmagic致力于将这种技术的实现成本降低,但是出于对资源提供者的尊重,webmagic不会做反封锁的事情,包括:验证码破解、代理切换、自动登录等。

webmagic的主要特色:

  • 完全模块化的设计,强大的可扩展性。
  • 核心简单但是涵盖爬虫的全部流程,灵活而强大,也是学习爬虫入门的好材料。
  • 提供丰富的抽取页面API。
  • 无配置,但是可通过POJO+注解形式实现一个爬虫。
  • 支持多线程。
  • 支持分布式。
  • 支持爬取js动态渲染的页面。
  • 无框架依赖,可以灵活的嵌入到项目中去。

webmagic的架构和设计参考了以下两个项目,感谢以下两个项目的作者:

python爬虫 scrapy https://github.com/scrapy/scrapy

Java爬虫 Spiderman http://git.oschina.net/l-weiwei/spiderman

webmagic的github地址:https://github.com/code4craft/webmagic

快速开始

使用maven

webmagic使用maven管理依赖,在项目中添加对应的依赖即可使用webmagic:

<dependency>
    <groupId>us.codecraft</groupId>
    <artifactId>webmagic-core</artifactId>
    <version>0.5.3</version>
</dependency>
<dependency>
    <groupId>us.codecraft</groupId>
    <artifactId>webmagic-extension</artifactId>
    <version>0.5.3</version>
</dependency>

WebMagic 使用slf4j-log4j12作为slf4j的实现.如果你自己定制了slf4j的实现,请在项目中去掉此依赖。

<exclusions>
    <exclusion>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </exclusion>
</exclusions>

项目结构

webmagic主要包括两个包:

  • webmagic-core

    webmagic核心部分,只包含爬虫基本模块和基本抽取器。webmagic-core的目标是成为网页爬虫的一个教科书般的实现。

  • webmagic-extension

    webmagic的扩展模块,提供一些更方便的编写爬虫的工具。包括注解格式定义爬虫、JSON、分布式等支持。

webmagic还包含两个可用的扩展包,因为这两个包都依赖了比较重量级的工具,所以从主要包中抽离出来,这些包需要下载源码后自己编译::

  • webmagic-saxon

    webmagic与Saxon结合的模块。Saxon是一个XPath、XSLT的解析工具,webmagic依赖Saxon来进行XPath2.0语法解析支持。

  • webmagic-selenium

    webmagic与Selenium结合的模块。Selenium是一个模拟浏览器进行页面渲染的工具,webmagic依赖Selenium进行动态页面的抓取。

在项目中,你可以根据需要依赖不同的包。

不使用maven

在项目的lib目录下,有依赖的所有jar包,直接在IDE里import即可。

第一个爬虫

定制PageProcessor

PageProcessor是webmagic-core的一部分,定制一个PageProcessor即可实现自己的爬虫逻辑。以下是抓取osc博客的一段代码:

public class OschinaBlogPageProcesser implements PageProcessor {

    private Site site = Site.me().setDomain("my.oschina.net");

    @Override
    public void process(Page page) {
        List<String> links = page.getHtml().links().regex("http://my\\.oschina\\.net/flashsword/blog/\\d+").all();
        page.addTargetRequests(links);
        page.putField("title", page.getHtml().xpath("//div[@class='BlogEntity']/div[@class='BlogTitle']/h1").toString());
        page.putField("content", page.getHtml().$("div.content").toString());
        page.putField("tags",page.getHtml().xpath("//div[@class='BlogTags']/a/text()").all());
    }

    @Override
    public Site getSite() {
        return site;

    }

    public static void main(String[] args) {
        Spider.create(new OschinaBlogPageProcesser()).addUrl("http://my.oschina.net/flashsword/blog")
             .addPipeline(new ConsolePipeline()).run();
    }
}

这里通过page.addTargetRequests()方法来增加要抓取的URL,并通过page.putField()来保存抽取结果。page.getHtml().xpath()则是按照某个规则对结果进行抽取,这里抽取支持链式调用。调用结束后,toString()表示转化为单个String,all()则转化为一个String列表。

Spider是爬虫的入口类。Pipeline是结果输出和持久化的接口,这里ConsolePipeline表示结果输出到控制台。

执行这个main方法,即可在控制台看到抓取结果。webmagic默认有3秒抓取间隔,请耐心等待。

使用注解

webmagic-extension包括了注解方式编写爬虫的方法,只需基于一个POJO增加注解即可完成一个爬虫。以下仍然是抓取oschina博客的一段代码,功能与OschinaBlogPageProcesser完全相同:

@TargetUrl("http://my.oschina.net/flashsword/blog/\\d+")
public class OschinaBlog {

    @ExtractBy("//title")
    private String title;

    @ExtractBy(value = "div.BlogContent",type = ExtractBy.Type.Css)
    private String content;

    @ExtractBy(value = "//div[@class='BlogTags']/a/text()", multi = true)
    private List<String> tags;

    public static void main(String[] args) {
        OOSpider.create(
        	Site.me(),
			new ConsolePageModelPipeline(), OschinaBlog.class).addUrl("http://my.oschina.net/flashsword/blog").run();
    }
}

这个例子定义了一个Model类,Model类的字段'title'、'content'、'tags'均为要抽取的属性。这个类在Pipeline里是可以复用的。

详细文档

http://webmagic.io/docs/

示例

webmagic-samples目录里有一些定制PageProcessor以抽取不同站点的例子。

webmagic的使用可以参考:oschina openapi 应用:博客搬家

协议

webmagic遵循Apache 2.0协议

贡献者:

以下是为WebMagic提交过代码或者issue的朋友:

邮件组:

Gmail: https://groups.google.com/forum/#!forum/webmagic-java

QQ: http://list.qq.com/cgi-bin/qf_invite?id=023a01f505246785f77c5a5a9aff4e57ab20fcdde871e988

QQ群:

373225642

![logo](https://raw.github.com/code4craft/webmagic/master/assets/logo.jpg) [Readme in Chinese](https://github.com/code4craft/webmagic/tree/master/README-zh.md) [User Manual (Chinese)](https://github.com/code4craft/webmagic/blob/master/user-manual.md) [![Build Status](https://travis-ci.org/code4craft/webmagic.png?branch=master)](https://travis-ci.org/code4craft/webmagic) >A scalable crawler framework. It covers the whole lifecycle of crawler: downloading, url management, content extraction and persistent. It can simplify the development of a specific crawler. ## Features: * Simple core with high flexibility. * Simple API for html extracting. * Annotation with POJO to customize a crawler, no configuration. * Multi-thread and Distribution support. * Easy to be integrated. ## Install: Add dependencies to your pom.xml: ```xml <dependency> <groupId>us.codecraft</groupId> <artifactId>webmagic-core</artifactId> <version>0.5.3</version> </dependency> <dependency> <groupId>us.codecraft</groupId> <artifactId>webmagic-extension</artifactId> <version>0.5.3</version> </dependency> ``` WebMagic use slf4j with slf4j-log4j12 implementation. If you customized your slf4j implementation, please exclude slf4j-log4j12. ```xml <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> ``` ## Get Started: ### First crawler: Write a class implements PageProcessor. For example, I wrote a crawler of github repository infomation. ```java public class GithubRepoPageProcessor implements PageProcessor { private Site site = Site.me().setRetryTimes(3).setSleepTime(1000); @Override public void process(Page page) { page.addTargetRequests(page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all()); page.putField("author", page.getUrl().regex("https://github\\.com/(\\w+)/.*").toString()); page.putField("name", page.getHtml().xpath("//h1[@class='entry-title public']/strong/a/text()").toString()); if (page.getResultItems().get("name")==null){ //skip this page page.setSkip(true); } page.putField("readme", page.getHtml().xpath("//div[@id='readme']/tidyText()")); } @Override public Site getSite() { return site; } public static void main(String[] args) { Spider.create(new GithubRepoPageProcessor()).addUrl("https://github.com/code4craft").thread(5).run(); } } ``` * `page.addTargetRequests(links)` Add urls for crawling. You can also use annotation way: ```java @TargetUrl("https://github.com/\\w+/\\w+") @HelpUrl("https://github.com/\\w+") public class GithubRepo { @ExtractBy(value = "//h1[@class='entry-title public']/strong/a/text()", notNull = true) private String name; @ExtractByUrl("https://github\\.com/(\\w+)/.*") private String author; @ExtractBy("//div[@id='readme']/tidyText()") private String readme; public static void main(String[] args) { OOSpider.create(Site.me().setSleepTime(1000) , new ConsolePageModelPipeline(), GithubRepo.class) .addUrl("https://github.com/code4craft").thread(5).run(); } } ``` ### Docs and samples: Documents: [http://webmagic.io/docs/](http://webmagic.io/docs/) The architecture of webmagic (refered to [Scrapy](http://scrapy.org/)) ![image](http://code4craft.github.io/images/posts/webmagic.png) Javadocs: [http://code4craft.github.io/webmagic/docs/en/](http://code4craft.github.io/webmagic/docs/en/) There are some samples in `webmagic-samples` package. ### Lisence: Lisenced under [Apache 2.0 lisence](http://opensource.org/licenses/Apache-2.0) ### Contributors: Thanks these people for commiting source code, reporting bugs or suggesting for new feature: * [ccliangbo](https://github.com/ccliangbo) * [yuany](https://github.com/yuany) * [yxssfxwzy](https://github.com/yxssfxwzy) * [linkerlin](https://github.com/linkerlin) * [d0ngw](https://github.com/d0ngw) * [xuchaoo](https://github.com/xuchaoo) * [supermicah](https://github.com/supermicah) * [SimpleExpress](https://github.com/SimpleExpress) * [aruanruan](https://github.com/aruanruan) * [l1z2g9](https://github.com/l1z2g9) * [zhegexiaohuozi](https://github.com/zhegexiaohuozi) * [ywooer](https://github.com/ywooer) * [yyw258520](https://github.com/yyw258520) * [perfecking](https://github.com/perfecking) * [lidongyang](http://my.oschina.net/lidongyang) * [seveniu](https://github.com/seveniu) * [sebastian1118](https://github.com/sebastian1118) * [codev777](https://github.com/codev777) * [fengwuze](https://github.com/fengwuze) ### Thanks: To write webmagic, I refered to the projects below : * **Scrapy** A crawler framework in Python. [http://scrapy.org/](http://scrapy.org/) * **Spiderman** Another crawler framework in Java. [http://git.oschina.net/l-weiwei/spiderman](http://git.oschina.net/l-weiwei/spiderman) ### Mail-list: [https://groups.google.com/forum/#!forum/webmagic-java](https://groups.google.com/forum/#!forum/webmagic-java) [http://list.qq.com/cgi-bin/qf_invite?id=023a01f505246785f77c5a5a9aff4e57ab20fcdde871e988](http://list.qq.com/cgi-bin/qf_invite?id=023a01f505246785f77c5a5a9aff4e57ab20fcdde871e988) QQ Group: 373225642 [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/code4craft/webmagic/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

简介

webmagic的是一个无须配置、便于二次开发的爬虫框架,它提供简单灵活的API,只需少量代码即可实现一个爬虫。 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/JimSow_admin/webmagic.git
git@gitee.com:JimSow_admin/webmagic.git
JimSow_admin
webmagic
webmagic
master

搜索帮助