五月天激情丁香,国产精品2019,国产成人精品亚洲2020,国产精品免费视频一区二区三区,开心久久婷婷综合中文字幕,天堂视频在线观看免费完整版

Spider

Spider組件可以方便用戶快速搭建分布式多協(xié)程爬蟲,用戶只需關(guān)心product和consume,product對dom的解析推薦使用Querylist

安裝

composer require easyswoole/spider

快速使用

以百度搜索為例,根據(jù)搜索關(guān)鍵詞爬出每次檢索結(jié)果前幾頁的特定數(shù)據(jù) 純屬教學(xué)目的,如有冒犯貴公司還請及時通知,會及時調(diào)整

Product

<?php
namespace App\Spider;

use EasySwoole\HttpClient\HttpClient;
use EasySwoole\Spider\Config\ProductConfig;
use EasySwoole\Spider\Hole\ProductAbstract;
use EasySwoole\Spider\ProductResult;
use QL\QueryList;
use EasySwoole\FastCache\Cache;

class ProductTest extends ProductAbstract
{

    public function product():ProductResult
    {
        // TODO: Implement product() method.
        // 請求地址數(shù)據(jù)
        $httpClient = new HttpClient($this->productConfig->getUrl());
        $httpClient->setHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36');
        $body = $httpClient->get()->getBody();

        // 先將每個搜索結(jié)果的a標(biāo)簽內(nèi)容拿到
        $rules = [
            'search_result' => ['.c-container .t', 'text', 'a']
        ];
        $searchResult = QueryList::rules($rules)->html($body)->query()->getData();

        $data = [];
        foreach ($searchResult as $result) {
            $item = [
                'href' => QueryList::html($result['search_result'])->find('a')->attr('href'),
                'text' => QueryList::html($result['search_result'])->find('a')->text()
            ];
            $data[] = $item;
        }

        $productJobOtherInfo = $this->productConfig->getOtherInfo();

        // 下一批任務(wù)
        $productJobConfigs = [];
        if ($productJobOtherInfo['page'] === 1) {
            for($i=1;$i<5;$i++) {
                $pn = $i*10;
                $productJobConfig = [
                    'url' => "https://www.baidu.com/s?wd={$productJobOtherInfo['word']}&pn={$pn}",
                    'otherInfo' => [
                        'word' => $productJobOtherInfo['word'],
                        'page' => $i+1
                    ]
                ];
                $productJobConfigs[] = $productJobConfig;
            }

            $word = Cache::getInstance()->deQueue(self::SEARCH_WORDS);
            if (!empty($word)) {
                $productJobConfigs[] = [
                    'url' => "https://www.baidu.com/s?wd={$word}&pn=0",
                    'otherInfo' => [
                        'word' => $word,
                        'page' => 1
                    ]
                ];
            }

        }

        $result = new ProductResult();
        $result->setProductJobConfigs($productJobConfigs)->setConsumeData($data);
        return $result;
    }

}

Consume

我這里直接存文件了,可按照需求自己定制

<?php
namespace App\Spider;

use EasySwoole\Spider\ConsumeJob;
use EasySwoole\Spider\Hole\ConsumeAbstract;

class ConsumeTest extends ConsumeAbstract
{

    public function consume()
    {
        // TODO: Implement consume() method.
        $data = $this->getJobData();

        $items = '';
        foreach ($data as $item) {
            $items .= implode("\t", $item)."\n";
        }

        file_put_contents('baidu.txt', $items, FILE_APPEND);
    }
}

注冊爬蟲組件

public static function mainServerCreate(EventRegister $register)
{
        $spiderConfig = [
            'product' => ProductTest::class, // 必須
            'consume' => ConsumeTest::class, // 必須
            'queueType' => SpiderConfig::QUEUE_TYPE_FAST_CACHE, // 通信類型默認(rèn)是fast-cache不支持分布式,如需分布式可使用SpiderConfig::QUEUE_TYPE_REDIS,或者自行實現(xiàn)通信隊列
            'queue' => '自定義隊列,如使用組件自帶則不需要', // 自定義通信隊列
            'queueConfig' => '自定義隊列配置,目前只有SpiderConfig::QUEUE_TYPE_REDIS需要',
            'maxCurrency' => 128 // 最大協(xié)程并發(fā)數(shù)(單臺機器)
        ];
        SpiderServer::getInstance()
            ->setSpiderConfig($spiderConfig)
            ->attachProcess(ServerManager::getInstance()->getSwooleServer());
}

投遞任務(wù)

$words = [
    'php',
    'java',
    'go'
];

foreach ($words as $word) {
    Cache::getInstance()->enQueue('SEARCH_WORDS', $word);
}

$wd = Cache::getInstance()->deQueue('SEARCH_WORDS');

SpiderClient::getInstance()->addJob(
                'https://www.baidu.com/s?wd=php&pn=0',
                [
                    'page' => 1,
                    'word' => $wd
                ]
);
主站蜘蛛池模板: 96一级毛片 | 天堂婷婷 | 久青草国产免费观看 | 四虎影院在线播放视频 | 都市激情男人天堂 | 中文一级国产特级毛片视频 | 久久成人国产精品二三区 | 奇米777影院| 99成人国产精品视频 | 免费a级毛片在线观看 | 午夜精品久久久 | 久久人人爽人人爽人人片669 | 男人天堂2016 | 人人精品久久 | 麻豆成人精品国产免费 | 国产99r视频精品免费观看 | 热re66久久精品国产99热 | 一级毛片免费看 | 国产真实各种门事件网站 | 五月天综合网 | 啪啪一级片 | 成人小视频免费在线观看 | 四库国产精品成人 | 日本激情网址 | 王朝的女人在线观看免费完整 | 99久久综合国产精品免费 | 狠狠干夜夜爽 | 99re在线精品视频 | www.日本免费 | 日韩在线视频免费不卡一区 | www.99在线| 久久婷婷五色综合夜啪 | 成 人 亚洲 综合天堂 | 2021国产精品自拍 | 2021中文字幕亚洲精品 | 99久久国产综合精品网成人影院 | 99r在线视频 | 免费观看国产精品视频 | 99视频免费播放 | 精品九九视频 | 国产精品免费视频一区二区三区 |