hanson/crawler

0.2.51 2016-06-05 08:19 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:43:59 UTC


README

一个简单的包,用于爬取网站列表和详细信息

安装

composer require hanccc/crawler

用法

此包需要 Goutte,您可以通过 $this->crawler(); 在列表和详细信息中获取DOM。

示例

        //or $listCrawler = new ExampleListCrawler(storage_path('logs'));
        $listCrawler = new ExampleListCrawler('http://example.com', storage_path('logs'));
        $listCrawler->setDetailCrawler(new ExampleDetailCrawler());
        $listCrawler->start();

列表爬取器

class ExampleListCrawler extends ListCrawler{
    public $url = 'http://example.com';
    
    //return links per page
    public function getEachPageUrl($page)
    {
        return 'http://example.com/list&page=' . $page;
    }
    
    // get the maximum number of pages
    public function setMaxPage()
    {
        $this->maxPage = $num;
    }
}

详情爬取器

class ExampleDetailCrawler extends DetailCrawler{

    //Returns boolean
    public function isDetailUrl($url)
    {
        if(preg_match('/example.com\/id(\d+)/, $url))
            return true;
    }
    
    // what you want to do about the detail page
    public function handle()
    {
        echo $this->crawler->filter('title')->text();
    }
}

许可协议

Crawler 是开源软件,遵循 MIT 许可协议