daa/web-scraping-sdk
Composer 安装命令:
composer require daa/web-scraping-sdk
包简介
Composer package that simplifies web scraping
README 文档
README
This is a composer package that simplifies web content scraping providing a lightweight and easy to use code base.
Simply extend the Scraper class provided and implement the gather() method to extract the desired content using xpaths. You can then output this content to a file, store in a database, return a json string, etc.
Highlights:
- XPath driven extraction of content
- Just one method to implement
- Allows easy file writing, database storage or formatted string/object return
- PSR2 coding standards
- Uses cURL to retrieve content from specified source
- Configurable failed attempts retry count and pause time
- Easily follow links to get additional content
Packagist link: https://packagist.org/packages/daa/web-scraping-sdk
Usage
Add the following requirement to your composer file and do a composer install/update:
"require": {
...
"daa/web-scraping-sdk: "1.*"
},
Write your own scraper class which extends Scraper\Sdk\WebScraper and implements the gather method:
namespace Your\Package\Scraper;
use Scraper\Sdk\WebScraper;
class YourScraper extends WebScraper
{
/**
* {@inheritdoc}
*/
protected function gather(\DOMXPath $dom)
{
$nodes = $dom->query(".//article[@class='product']");
foreach ($nodes as $node) {
...
// follow a url and extract more data
$linkDom = $this->getLinkContent($node->getElementsByTagName('a')->item(0));
$linkDom->query...
}
}
}
Now call your class, for example from a script that is executed by a cron job:
require __DIR__.'/../vendor/autoload.php';
$scraper = new Your\Package\Scraper\YourScraper('http://www.someurl.com/with/content/');
$scraper->execute();
With troublesome sources you can specify the retry configuration (default is 3 retries with a 3 second pause in between)
$scraper = new Your\Package\Scraper\YourScraper('http://www.someurl.com/with/content/', $retryAttempts, $pauseSeconds);
$scraper->execute();
You can use the same instance to scrape several urls with the same structure:
$pages = array(
'http://www.someurl.com/section-one/',
'http://www.someurl.com/section-two/page1',
'http://www.someurl.com/section-one/page2'
);
$scraper = new Your\Package\Scraper\YourScraper();
foreach ($pages as $url) {
$scraper->setSource($url);
$scraper->execute();
}
Check out the examples folder for more details and fully working examples.
daa/web-scraping-sdk 适用场景与选型建议
daa/web-scraping-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 2, 最近一次更新时间为 2014 年 12 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 daa/web-scraping-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 daa/web-scraping-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 58
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-12-21