<?php
include __DIR__ . '/vendor/autoload.php';//引入自動載入類
use Goutte\Client;//使用第三方採集庫
$client = new Client();
$links = [
'http://www.nipic.com/topic/show_27192_1.html',
'http://www.nipic.com/topic/show_27054_1.html',
'http://www.nipic.com/topic/show_27085_1.html',
];//要爬的三個分類下的圖片
$pids = [];//定義一個子程式的陣列
foreach ($links as $url) {
$pid = pcntl_fork();//fork 一個子程式
switch ($pid) {
case -1:
die("Fork failed\n");
case 0:
$id = posix_getpid();//得到子程式id
$pids[$id] = $id;//存入到子程式陣列id 中
$data = [];
$crawler = $client->request('GET', $url);
$crawler->filter('.search-works-thumb')->each(function($node) use ($client, $id, &$data) {
$url = $node->link()->getUri();
$crawler = $client->request('GET', $url);
$crawler->filter('#J_worksImg')->each(function($node) use ($id, &$data) {
$src = $node->image()->getUri();
$data[$id][] = $src;//存入訊息佇列 或者redis 都可以
//echo $src . PHP_EOL;
});
});
print_r($data);
exit;//結束子程式
break;
default:
sleep(2);
break;
}
}
while ( count($pids) ) {
if (($id = pcntl_wait($status, WUNTRACED)) > 0) {//主程式 阻塞等待子程式完成任務
unset($pids[$id]);
}
}
echo "Done\n";
本作品採用《CC 協議》,轉載必須註明作者和本文連結