本文實例講述了php使用pthreads v3多線程實現(xiàn)抓取新浪新聞信息。分享給大家供大家參考,具體如下:
我們使用pthreads,來寫一個多線程的抓取頁面小程序,把結(jié)果存到數(shù)據(jù)庫里。
數(shù)據(jù)表結(jié)構(gòu)如下:
CREATE TABLE `tb_sina` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `url` varchar(256) DEFAULT '' COMMENT 'url地址', `title` varchar(128) DEFAULT '' COMMENT '標(biāo)題', `time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '時間', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='sina新聞';
代碼如下:
?php class DB extends Worker { private static $db; private $dsn; private $root; private $pwd; public function __construct($dsn, $root, $pwd) { $this->dsn = $dsn; $this->root = $root; $this->pwd = $pwd; } public function run() { //創(chuàng)建連接對象 self::$db = new PDO($this->dsn, $this->root, $this->pwd); //把require放到worker線程中,不要放到主線程中,不然會報錯找不到類 require './vendor/autoload.php'; } //返回一個連接資源 public function getConn() { return self::$db; } } class Sina extends Thread { private $name; private $url; public function __construct($name, $url) { $this->name = $name; $this->url = $url; } public function run() { $db = $this->worker->getConn(); if (empty($db) || empty($this->url)) { return false; } $content = file_get_contents($this->url); if (!empty($content)) { //獲取標(biāo)題,地址,時間 $data = QL\QueryList::Query($content, [ 'tit' => ['.c_tit > a', 'text'], 'url' => ['.c_tit > a', 'href'], 'time' => ['.c_time', 'text'], ], '', 'UTF-8', 'GB2312')->getData(); //把獲取的數(shù)據(jù)插入數(shù)據(jù)庫 if (!empty($data)) { $sql = 'INSERT INTO tb_sina(`url`, `title`, `time`) VALUES'; foreach ($data as $row) { //修改下時間,新浪的時間格式是這樣的04-23 15:30 $time = date('Y') . '-' . $row['time'] . ':00'; $sql .= "('{$row['url']}', '{$row['tit']}', '{$time}'),"; } $sql = rtrim($sql, ','); $ret = $db->exec($sql); if ($ret !== false) { echo "線程{$this->name}成功插入{$ret}條數(shù)據(jù)\n"; } else { var_dump($db->errorInfo()); } } } } } //抓取頁面地址 $url = 'http://roll.news.sina.com.cn/s/channel.php?ch=01#col=89spec=type=ch=01k=offset_page=0offset_num=0num=60asc=page='; //創(chuàng)建pool池 $pool = new Pool(5, 'DB', ['mysql:dbname=test;host=192.168.33.226', 'root', '']); //獲取100個分頁數(shù)據(jù) for ($ix = 1; $ix = 100; $ix++) { $pool->submit(new Sina($ix, $url . $ix)); } //循環(huán)收集垃圾,阻塞主線程,等待子線程結(jié)束 while ($pool->collect()) ; $pool->shutdown();
由于使用到了QueryList,大家可以通過composer進(jìn)行安裝。
composer require jaeger/querylist
不過安裝的版本是3.2,在我的php7.2下會有問題,由于each()已經(jīng)被廢棄,所以修改下源碼,each()全換成foreach()就好了。
運行結(jié)果如下:
數(shù)據(jù)也保存進(jìn)了數(shù)據(jù)庫
當(dāng)然大家也可以再次通過url,拿到具體的頁面內(nèi)容,這里就不做演示了,有興趣的可以自已去實現(xiàn)。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP進(jìn)程與線程操作技巧總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
標(biāo)簽:唐山 通遼 南通 黃山 河南 平頂山 武漢 隴南
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php使用pthreads v3多線程實現(xiàn)抓取新浪新聞信息操作示例》,本文關(guān)鍵詞 php,使用,pthreads,多,線程,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。