25 Star 47 Fork 18

gitclebeg / nlp-spider-dynamic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

nlp-spider-dynamic

专门为自然语言处理系统组件爬取数据的组件,动态信息爬取的网络爬虫

这个爬虫的特点是主要针对动态网页信息爬取的垂直爬虫。设计思路与八爪鱼采集器类似。

注意此爬虫纯属个人开发兴趣而开发,有任何问题欢迎各位提交bug。邮箱地址:276708284@qq.com

目前正在完善,有些功能可能有bug,敬请期待...

1、说明

动态网站的抓取和静态网站抓取最大的不同就是要解析JS。

    1. 一种方式是跟踪JS的真实请求(难度大)
    1. 一种方式是直接模拟浏览器解析JS 本程序采用第二种方式,利用Selenium浏览器测试框架,实现了一个可以解析页面JS的 web 网络爬虫,从而可以抓取动态网页信息。

2、新手教程

  1. driver 目录(存放的是selenium需要运行的driver) 不同的平台,请参考这个教程,自行编译 里面目前只是提供windows GhostDriver 已经集成在 Phantomjs.exe

  2. 简单爬取例子 在tasks\test_ghost下面的配置文件分别举例说明如何配置爬虫抓取各种网页,下面是每个文件的说明:

jd_1product.xml				演示如何抓取一个简单商品页上的信息(商品名,动态评论数)
jd_products_comments.xml	演示如何抓取一个商品的评论(内容循环提取)
jd_shop_allproducts.cml		将演示如何循环抓取一个店铺的所有商品(翻页循环抓取)

其它任务类似,数据保存的名字,以及方式都是在配置文件中定义的。 配置好一只爬虫之后,启动程序爬虫就会自动生成需要保存的数据文件。

  1. 爬虫执行 执行 org.wisdomdata.main.MainCrawler 将配置文件所在地址添加到 tasksFiles 即可启动程序,目前只能支持一只爬虫,将来可能会支持分布式爬虫。

3、架构设计

采用Selenium,一个最大的限制是多线程的并发控制。 最好的方式是采用WebDriver单例模式。采用Spring框架可以容易实现。

垂直网络爬虫不仅仅只是下载页面,下载页面只是它的一个组件。

    1. 页面处理
还需要针对性的页面进行处理,比如:
在页面上点击一个按钮(本质是执行一段JS);
在页面上跳转直接(比如:加载一个新的页面)
碰到特殊情况处理(比如:弹出验证码输入框,提取不到页面内容等等)
页面内容提取(包括链接,以及主题指标数据)
    1. 链接处理
如何保存批量提取的链接?
垂直爬虫得到的链接比较少量。
    1. 爬取策略
如何增量爬取数据?
如何解决爬虫被封的问题?

这些过程抽象出来,非常符合编程语言里面的三大程序流程结构:

1、顺序 2、循环 3、条件

由这三个组合起来就可以完成任何复杂的程序流程。

一个垂直网站的爬取,也可以用上面的三个过程组合起来进行描述: 比如在爬取一个网站的时候:

do for 循环列表页(循环方式 点击下一页,结束条件(指定最大次数等))
	do for 每一个内容页面(循环方式 依次抓取,结束条件)
		提取信息(异步提取,同步提取)
	done
点击下一页
done

比如更加复杂一点的网站:

do
	填写表单,点击提交,指定页面跳转
done

do then
	do 循环列表页(循环方式 点击下一页,结束条件(指定最大次数等))
		提取信息
	done
	do then
		点击某个按钮
		do
			do for 循环内容
				提取信息(
					if (未提取到信息,或者提取到信息有什么问题)
						dosome
					endif
			done
		done
	点击按钮
	done
done

基于这样的逻辑,开发了这个系统。

4、如何结合设计一只爬虫

爬虫设计抽象出来就是程序设计语言都有的三个处理结构。

  • 1) 业务逻辑处理器(包含多个内容抽取器)
判断(需要判断条件,符合就执行,不符合就执行另外的)
循环(需要开始条件,循环间隔,循环结束条件)
顺序(就是一系列执行下去)
  • 2) 内容抽取器(底层由一系列原子抽取动作组成,这些原始抽象动作都是需要先满足一定条件的)
判断(需要判断条件,符合就执行,不符合就执行另外的)
循环(需要开始条件,循环间隔,循环结束条件)
顺序(就是一系列执行下去)
  • 3) 状态转移动作 *

每一个状态转移动作之后都会产生一个新的页面,而且跳转之前都需要判断

比如:(事务支持,要么原子动作全部执行,要么全部失败)

点击下一页(是否存在点击下一页这个按钮,没有怎么办?)
点击某个按钮加载一段新页面(是否存在需要这个按钮,没有怎么办?)
跳转到一个新的页面(新页面链接是否是有效链接?)

(上面的每一个动作里面包含原子动作,执行某个点击按钮的动作)

  • 4) 抽取信息去重保存组件

上面每一个处理过程都是嵌套的,也就是说可能循环里面有判断,判断里面有循环等等 ####而且相对于爬虫来说: ####上面的每一个处理过程,都必须打开一个页面作为基准。

业务逻辑处理器里面包含转移动作组合
转移动作组里面包含系列内容抽取器
内容抽取器里面可能包含业务逻辑组

4.1 举例说明:

1) 单独爬取一个网站,需要的组件
顺序执行处理器1(
    跳转到一个新页面的转移动作(
        顺序抽取器(多个单元抽取器,去重保存组件)



2) 登录页面抓取
顺序执行处理器2(
    登录动作组(无抽取动作)
    顺序执行处理器1


3) 循环页面抓取
循环执行处理器2(
    循环初始化条件
    循环执行动作(抽取器)
    循环结束条件


4) 页面点击抓取
顺序执行处理器3(
    跳转到一个新页面的转移动作(
        顺序抽取器(多个单元抽取器,去重保存组件)

    点击页面按钮动作组(
         顺序抽取器(多个单元抽取器,去重保存组件)?

这上面四种情况组合起来还能组合成更加复杂的情况。 抽象处理就是:

  1. 处理器里面有动作组
  2. 每个动作里面有抽取器组
  3. 每个抽取器里面有保存组件以及原子抽取器

5、项目相关信息

一只可以抓取动态信息的爬虫高定制爬虫

一只神奇的爬虫

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2015 gitclebeg Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

nlp项目基础框架:爬虫,针对动态网页(JS)的专有爬虫 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/gitclebeg/nlp-spider-dynamic.git
git@gitee.com:gitclebeg/nlp-spider-dynamic.git
gitclebeg
nlp-spider-dynamic
nlp-spider-dynamic
master

搜索帮助