定制 robertbyrnes/phpcrawler 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

robertbyrnes/phpcrawler

Composer 安装命令:

composer require robertbyrnes/phpcrawler

包简介

A PHP CLI links crawler using FIFO Queue data structure and Producer/Consumer model.

README 文档

README

Implementation of Queue - Producer - Consumer Web Crawler in PHP. Uses multiple processes or native threads via the amphp/parallel dependency to crawl a domain for respondant links.

                                                    / _ \
                                                  \_\(_)/_/
                                                   _//o|\_
                                                    /  |

@author: Robert Byrnes @email: robert@globalrbdev.uk

Install

Install using 'composer require robertbyrnes/phpcrawler': Once installed 'cd' into vendor/robertbyrnes/phpcrawler to find main.php this is the file to run the program. If you run into any trouble with 'class not found' errors be sure to run composer update and composer dump-autoload commands. If run with 'php main.php' from a terminal/command prompt the help menu will show detailing the arguments required to begin a crawl.

/*** ARGUMENTS ***/
Required arguments:
    -u url (string) e.g. http://website.org or https://...
    -n project name (string) e.g. website - this is used to create the dir to save the results to
        following crawling.
Optional arguments: 
    -s number of spiders (integer)[parallel processes] used in crawling. Default is 4.
    -v returns the version.
    -h prints this help message.

Dependencies

This program requires ^PHP7 to run as well as the amphp/parallel library for the multiple processes. Amphp/parallel should auto install with composer.

Classes

Crawler::class

Manages queueing of tasks and passes work between Queue::class and Spider::class utilising producer/consumer model with queue.

  • Crawler::class functions:

  • spawn() recursive function implementing the functionality of the parallel library to create processes which call Spider::search() to the do the crawling. This recursive loop will run until the Queue::class->queue is empty. When this happens the program will exit.

  • add_job() Reads queue.txt using SaveData::class adding links to the queue pushing the links to the Queue::class. This function is recursive one of two programming loops.

Queue::class

A first-in, first-out data structure.

  • Queue::class functions:

  • push() pushes an item to the end of the queue.

  • shift() take an item off the head of the queue.

  • pop() take an item off the end of the queue.

  • open_job() increment the count of unfinished tasks.

  • task_done() decrement the count of unfinished tasks.

  • getCount() gets the total number of items in the queue.

SaveData::class

Handles all file tasks.

  • SaveData::class functions:

  • create_dir() creates the project dir within results in the root.

  • create_files() creates queue.txt and crawled.txt in the project dir.

  • file_to_array() opens either queue.txt or crawled.txt and parses stream to array.

  • array_to_file() takes queue array or crawled array and writes to queue.txt or crawled.txt.

  • write_file() uses fwrite to open new files, or open - empty - then rewrite.

  • append_to_file() appends lines to either queue.txt or crawled.txt.

  • delete_file_contents() empties files - unused in PHP-Crawler.

Spider::class

Extracts links from given url. Updates queues and files.

  • Spider::class functions:

  • setup() prints to user the domain name derived from the url. Creates dir and files. Populates both queue and crawled arrays from files.

  • search() takes a url as an argument and calls extract_links(), passing the result to sort_to_queue(). Prints updated queue counts to the user and updates the files once the crawling round is complete.

  • getDomain() extracts the domain name form the given url.

  • extract_links() uses the built in php DomDocument::class to parses links extracted from a url into an array.

  • sort_to_queue() cleans link to ensure all are from within the domain. Ensures unique links are pushed to the queue in Queue::class.

  • update() utilises SaveData::class to write files queue.txt and crawled.txt with updated links.

  • check_queue() checks the count of the Spider::class property $queue. Exits the program once the queue is empty.

robertbyrnes/phpcrawler 适用场景与选型建议

robertbyrnes/phpcrawler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「cli」 「crawler」 「Web Links」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 robertbyrnes/phpcrawler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 robertbyrnes/phpcrawler 我们能提供哪些服务?
定制开发 / 二次开发

基于 robertbyrnes/phpcrawler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2021-03-29