1 Star 0 Fork 54

oceanfloyd / webmagicx

forked from luosl / webmagicx 
Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README

webmagicx

webmagicx一款基于webmagic的可配置化的爬虫框架

webmagicx是一款可配置爬虫框架,webmagicx中的webmagic表示该框架扩展于webmagic,x表示该框架是一个基于xml的配置型爬虫框架。 得益于webmagic强大的可扩展能力,本框架实现了以下特性:

  • 无需写任何代码,只需你熟悉正则表达式和xpath,通过简单的配置便可实现一个爬虫。
  • 实现了基于corn的定时调度功能。
  • 提供了文本替换、正则查找、拆分等处理逻辑
  • 提供了文件下载功能。
  • 提供了简单通用存储功能,能够将抓取的数据轻松存入数据库和文件。
  • 在webmagic的基础上实现了深度抓取。
  • 基于rest的监控
  • 多线程支持。

要求

jdk1.8以上

快速开始

使用二进制包开始第一个爬虫

webmagicx提供了可执行的二进制包,免去了编译痛苦,直接下载就可以使用。二进制包中,内置了一个抓取豆瓣电影信息的爬虫配置文件,你只需要以下步骤,就可以启动你的第一个爬虫,抓取豆瓣电影:

webmagicx
└ spiderConf                      → 爬虫配置文件目录,用于存放爬虫配置文件
  └ douban.spider.xml             → 爬虫配置文件,请放到格式为xml,请放到spiderConf目录下,请务必以.spider.xml结尾
└ bin                             → 命令路径                          
  └ start.bat                     → windos环境下,快捷启动命令        
└conf                             → 项目配置
  └ log4j.properties              → 项目日志配置文件      
└ lib                             → 项目依赖
  • 运行bin/start.bat 命令
  • 待爬虫运行一段时间后会在当bin目录中 豆瓣影.csvimg 文件夹,分别存放了电影资料和电影封面图片。如下图所示: 电影信息 电影封面
  • 打开浏览器,输入网址http://localhost:9000/spider/douban.spider,即可看到爬虫的运行状态,如下图所示: rest监控

webmagicx的配置文件

爬虫配置是webmagicx的核心,至于配置文件选用xml格式的原因有以下几点:


  • xml非常的大众化,特别是对于广大的javaer而言。
  • xml虽然有些臃肿,但它的表达能力是毋庸置疑的,同时他也非常易于阅读,同时层次也非常的清晰。
  • 最后一点,我觉得scala解析xml真的很容易 :sunglasses:

说了这么多,其实配置也就这几个部分:

  • startUrls:爬虫url入口。
  • targetUrlRegexs:爬虫到底抓那些网页?
  • attribute:爬虫的属性,比如开几个线程,抓多深的深度等。
  • components:爬虫的组件爬虫,一般情况下,我们只需要定义pipeline,用来告诉爬虫怎么保存抓取的结果。
  • site:站点配置,主要是用来伪装爬虫。
  • fields:抓取的字段配置。

列举了这么,其实下面的配置文件就说的很清楚了。。。。

<?xml version="1.0" encoding="UTF-8" ?>
<!-- enable代表该爬虫是否启用 -->
<spider enable="true">
    <!-- 任务描述 -->
    <desc>抓豆瓣电影信息</desc>
    <!-- 定时任务配置,当startNow为true,任务会马上启动  -->
    <!--<task startNow="true" corn="0 0 */1 * * ?" />-->
    <!-- 起始url列表,可以配置多个 -->
    <startUrls>
        <url>https://movie.douban.com/subject/26662193/?from=showing</url>
    </startUrls>
    <!-- 目标url正则,可以配置多个 -->
    <targetUrlRegexs>
        <!-- xpath可以限定目标url的抽取范围 -->
        <regex xpath="//*div[@class='recommendations-bd']">https://movie\.douban\.com/subject.*</regex>
    </targetUrlRegexs>
    <!-- 爬虫属性 -->
    <attribute>
        <!-- 最大抓取深度,默认为-1 -->
        <maxDeep>1</maxDeep>
        <!-- 字符集,默认为UTF-8 -->
        <charset>UTF-8</charset>
        <!-- 超时(毫秒),默认为20000 -->
        <timeout>20000</timeout>
        <!-- 线程数,默认为1 -->
        <threadNum>3</threadNum>
        <!-- 抓取失败后的重试次数,默认为0 -->
        <retryTimes>0</retryTimes>
        <!-- 每次抓取后的睡眠时间,默认为1000 -->
        <sleep>1000</sleep>
    </attribute>
    <!-- 组件配置 -->
    <components>
        <!-- 下载处理 -->
        <downloadHandler>
            <!--
                | src   源字段:由field中配置,或handler中产生的字段名称)
                | target    目标字段 当目标字段已经存在时,原目标字段会被覆盖
                | retryTimes 重试次数:若下载失败,重试的次数
                | saveDir 文件保存文件夹;
                | returnValue 下载文件后的返回值,取值 md5、path、fileName;
                              分别表示文件md5值、文件绝对路径、文件名。
             -->
            <download src="imgUrl" target="imgPath" retryTimes="2" saveDir="img" returnValue="path" />
        </downloadHandler>
        <!-- 文本处理 -->
        <textHandler>
            <!-- 替换info字段中的/为, -->
            <!-- src :原字段,target:生成的目标字段,expression:需要替换的表达式,replaceAs:需要替换为的字段 -->
            <replaceAll src="info" target="info" expression=" / " replaceAs=","/>
            <!-- 导演 -->
            <!-- src :原字段,target:生成的目标字段,expression:正则表达式,takeIndex:取匹配到的第几个字段 -->
            <find src="info" target="dy" expression="导演: +[0-9a-zA-Z\u2E80-\u9FFF\.·\-,\)\(]+ " takeIndex="0"/>
            <split src="dy" target="dy" expression=" " takeIndex="1"/>
            <!-- 编剧 -->
            <find src="info" target="bj" expression="编剧: +[0-9a-zA-Z\u2E80-\u9FFF\.·\-,\)\(]+ " takeIndex="0"/>
            <replaceAll src="bj" target="bj" expression="编剧: " replaceAs=""/>
            <!-- 主演 -->
            <find src="info" target="zy" expression="主演: +[0-9a-zA-Z\u2E80-\u9FFF\.·\-,\)\(]+ " takeIndex="0"/>
            <replaceAll src="zy" target="zy" expression="主演: " replaceAs=""/>
            <!-- 类型 -->
            <find src="info" target="lx" expression="类型: +[0-9a-zA-Z\u2E80-\u9FFF\.·\-,\)\(]+ " takeIndex="0"/>
            <replaceAll src="lx" target="lx" expression="类型: " replaceAs=""/>
            <!-- 制片国家/地区 -->
            <find src="info" target="dq" expression="制片国家/地区: +[0-9a-zA-Z\u2E80-\u9FFF\.·\-,\)\(]+ " takeIndex="0"/>
            <replaceAll src="dq" target="dq" expression="制片国家/地区: " replaceAs=""/>
            <!-- 片长 -->
            <find src="info" target="pc" expression="片长: +[0-9a-zA-Z\u2E80-\u9FFF\.·\-,\)\(]+ " takeIndex="0"/>
            <replaceAll src="pc" target="pc" expression="片长: " replaceAs=""/>
            <!-- 又名 -->
            <find src="info" target="ym" expression="又名: .+IMDb链接" takeIndex="0"/>
            <replaceAll src="ym" target="ym" expression="IMDb链接|又名:" replaceAs=""/>
            <!-- IMDb链接 -->
            <find src="info" target="imdb" expression="IMDb链接: [0-9a-z]+" takeIndex="0" must="false"/>
            <replaceAll src="imdb" target="imdb" expression="IMDb链接: " replaceAs="" />
            <!-- 上映日期 -->
            <find src="info" target="syrq" expression="\d{4}-\d{2}-\d{2}" takeIndex="0" must="false"/>
        </textHandler>
        <!-- 管道配置 -->
        <!-- jdbc管道 -->
        <!--<jdbcPipeline>-->
            <!--&lt;!&ndash; 写模式,当前支持 override 和 append ,分别代表覆盖和追加 &ndash;&gt;-->
            <!--<model value="override"/>-->
            <!--&lt;!&ndash; 用来进行去重的字段,在 程序中内置[_url,_page]两个字段,分别表示当前url,和当前页面 &ndash;&gt;-->
            <!--&lt;!&ndash; field指的是配置的抓取字段名称,dbColumn表示映射在数据库的列名称 &ndash;&gt;-->
            <!--<distinct field="_url" dbColumn="url"/>-->
            <!--&lt;!&ndash; 是否自动建表 &ndash;&gt;-->
            <!--<autoCreateTable value="true"/>-->
            <!--&lt;!&ndash; 需要保存的字段, * 表示所有字段,_url:ur 表示将 抓取字段到数据库字段的映射 &ndash;&gt;-->
            <!--<needSaveFields value="_url:url,title,info,imgUrl"/>-->
            <!--&lt;!&ndash; 数据库驱动 &ndash;&gt;-->
            <!--<driver value="com.mysql.jdbc.Driver"/>-->
            <!--&lt;!&ndash; 数据库url &ndash;&gt;-->
            <!--<url value="jdbc:mysql://localhost/spider?useUnicode=true&amp;characterEncoding=UTF-8" />-->
            <!--&lt;!&ndash; 数据库用户名 &ndash;&gt;-->
            <!--<user value="root"/>-->
            <!--&lt;!&ndash; 数据库密码 &ndash;&gt;-->
            <!--<password value="123456"/>-->
            <!--&lt;!&ndash; 数据库表名称 &ndash;&gt;-->
            <!--<tableName  value="tb_douban"/>-->
        <!--</jdbcPipeline>-->
        <!-- csv管道 -->
        <csvPipeline>
            <!-- 写模式,目前支持 override 和 append,分别代表覆盖和追加 -->
            <model value="override"/>
            <charset value="gb2312" />
            <!-- 用来进行去重的字段,非必须,在程序中内置[_url,_page]两个字段,分别表示当前url,和当前页面 -->
            <distinct field="_url" csvHeader="url"/>
            <!-- 需要保存的字段,*表示所有字段 -->
            <needSaveFields value="title:电影标题,_url:url,dy:导演,bj:编剧,zy:主演,lx:类型,dq:地区,pc:片长,ym:别名,imdb,syrq:上映日期,imgUrl:封面地址,score:豆瓣评分,imgPath,abstract:简介"/>
            <!-- 在爬虫任务终止时是否需要关闭csv文件,当任务为一次性时请设置为true,为调度任务时,设置为false -->
            <closeAtTheEnd value="false" />
            <!-- 保存的csv路径 -->
            <path value="豆瓣电影.csv"/>
        </csvPipeline>
    </components>
    <!-- 站点配置 -->
    <site>
        <!-- 浏览器标识 -->
        <userAgent>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36</userAgent>
        <!-- 请求头 -->
        <headers>
            <header key="Host" value="movie.douban.com" />
        </headers>
        <!-- cookie -->
        <cookies>
            <cookie key="bid" value="xYD83ezN29E"/>
            <cookie key="gr_user_id" value="293a3df2-8d48-4e5b-8559-4f2b9084cd0b"/>
            <cookie key="ll" value="118318"/>
            <cookie key="__yadk_uid" value="y3wXc0nkdQ6WgQLoqigA5YsjTzM0jwj4"/>
            <cookie key="viewed" value="1942934_24703171_2698938_20266642_1909003_5373022_6847455_6536593_1425950"/>
            <cookie key=" ap" value="1"/>
        </cookies>
    </site>
    <!-- 抓取字段配置 -->
    <fields>
        <field>
            <!-- 字段名称 -->
            <name>title</name>
            <!--
            属性scope取值为head,last,all;别表示:取第一个有效值;取最后一个有效值,取所有有效值。
            -->
            <scope>head</scope>
            <!-- 抽取内容表达式,支持 xpath cssSelector;后续增加 jsonPath  -->
            <extract type="xpath" expression="//*div[@id='content']/h1/span[1]"/>
            <!-- 是否将抽取后的数据格式化为纯文本(剔除html标签) -->
            <textFormat>true</textFormat>
            <!-- 是否为必须字段,当为true时,若该字段未抓取到值,不会被pipelne处理 -->
            <must>true</must>
        </field>
        <field>
            <name>info</name>
            <extract type="xpath" expression="//*[@id='info']"/>
            <textFormat>true</textFormat>
            <must>true</must>
        </field>
        <field>
            <name>imgUrl</name>
            <extract type="xpath" expression="//*[@id='mainpic']/a/img/@src"/>
            <textFormat>true</textFormat>
            <must>true</must>
        </field>
        <field>
            <name>score</name>
            <extract type="xpath" expression="//*[@id='interest_sectl']//*[@class='ll rating_num']/text()"/>
            <textFormat>false</textFormat>
            <must>false</must>
        </field>
        <field>
            <name>abstract</name>
            <extract type="xpath" expression="//*[@id='link-report']//span//text()"/>
            <textFormat>false</textFormat>
            <must>false</must>
        </field>
    </fields>
</spider>

说明

项目才刚刚开始,大家有什么建议和想法欢迎一起交流。同时也希望有兴趣和精力的盆友一起来完善这个项目 :smile:

QQ群

468248192

Empty file

About

webmagicx一款基于webmagic的可配置化的爬虫框架 expand collapse
Scala
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
Scala
1
https://gitee.com/oceanfloyd/webmagicx.git
git@gitee.com:oceanfloyd/webmagicx.git
oceanfloyd
webmagicx
webmagicx
master

Search