thewinterwind / arachnid
用于查找给定网站上所有独特内部页面的爬虫
1.1
2016-12-25 22:59 UTC
Requires
- php: >=5.6.0
- fabpot/goutte: ^3.1
- monolog/monolog: ^1.22
Requires (Dev)
- phpunit/phpunit: ^4.6
- squizlabs/php_codesniffer: ~2.0
- symfony/var-dumper: ^2.7
This package is not auto-updated.
Last update: 2024-09-20 19:30:08 UTC
README
此库将爬取给定网站上找到的所有独特内部链接,最多到指定的最大页面深度。
此库基于Zeid Rashwani的原始博客文章,见此处
http://zrashwani.com/simple-web-spider-php-goutte
Josh Lockhart在得到许可的情况下修改了原始博客文章的代码,并将其用于Composer和Packagist,并更新了语法以符合PSR-2编码标准。
如何安装
您可以使用Composer安装此库。将其添加到您的composer.json
清单文件中
{
"require": {
"zrashwani/arachnid": "dev-master"
}
}
然后运行composer install
。
入门指南
以下是一个快速演示,用于爬取一个网站
<?php
require 'vendor/autoload.php';
$url = 'http://www.example.com';
$linkDepth = 3;
// Initiate crawl
$crawler = new \Arachnid\Crawler($url, $linkDepth);
$crawler->traverse();
// Get link data
$links = $crawler->getLinks();
print_r($links);
高级用法
您可以为爬虫设置其他选项
通过在构造函数中指定选项数组或将它传递给setCrawlerOptions
,设置底层guzzle客户端的额外选项
<?php
//third parameter is the options used to configure guzzle client
$crawler = new \Arachnid\Crawler('http://github.com',2,
['auth'=>array('username', 'password')]);
//or using separate method `setCrawlerOptions`
$options = array(
'curl' => array(
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
),
'timeout' => 30,
'connect_timeout' => 30,
);
$crawler->setCrawlerOptions($options);
您可以将符合PSR-3的记录对象注入以监控爬虫活动(例如Monolog
)
<?php
$crawler = new \Arachnid\Crawler($url, $linkDepth); // ... initialize crawler
//set logger for crawler activity (compatible with PSR-3)
$logger = new \Monolog\Logger('crawler logger');
$logger->pushHandler(new \Monolog\Handler\StreamHandler(sys_get_temp_dir().'/crawler.log'));
$crawler->setLogger($logger);
?>
通过使用filterLinks
方法指定回调闭包,设置爬虫只访问具有特定标准的页面
<?php
//filter links according to specific callback as closure
$links = $crawler->filterLinks(function($link){
//crawling only blog links
return (bool)preg_match('/.*\/blog.*$/u',$link);
})
->traverse()
->getLinks();
如何贡献
- 分叉此存储库
- 为每个功能或改进创建一个新的分支
- 将代码更改和相应的单元测试一起应用
- 从每个功能分支发送pull请求
将新功能或改进分离到单独的分支中,并为每个分支发送pull请求非常重要。这使我能够单独审查和合并新功能或改进。
所有pull请求都必须遵循PSR-2标准。
系统要求
- PHP 5.6.0+
作者
- Josh Lockhart https://github.com/codeguy
- Zeid Rashwani http://zrashwani.com
许可
MIT公共许可证