php使用pthreads v3多線程實現(xiàn)抓取新浪新聞信息操作示例
本文實例講述了php使用pthreads v3多線程實現(xiàn)抓取新浪新聞信息。分享給大家供大家參考,具體如下:
我們使用pthreads,來寫一個多線程的抓取頁面小程序,把結果存到數(shù)據(jù)庫里。
數(shù)據(jù)表結構如下:
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 '標題', `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)) { //獲取標題,地址,時間 $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=89&spec=&type=&ch=01&k=&offset_page=0&offset_num=0&num=60&asc=&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)收集垃圾,阻塞主線程,等待子線程結束 while ($pool->collect()) ; $pool->shutdown();
由于使用到了QueryList,大家可以通過composer進行安裝。
composer require jaeger/querylist
不過安裝的版本是3.2,在我的php7.2下會有問題,由于each()已經被廢棄,所以修改下源碼,each()全換成foreach()就好了。
運行結果如下:
數(shù)據(jù)也保存進了數(shù)據(jù)庫
當然大家也可以再次通過url,拿到具體的頁面內容,這里就不做演示了,有興趣的可以自已去實現(xiàn)。
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP進程與線程操作技巧總結》、《PHP網絡編程技巧總結》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
ThinkPHP模板標簽eq if 中區(qū)分0,null,false的方法
下面小編就為大家?guī)硪黄猅hinkPHP模板標簽eq if 中區(qū)分0,null,false的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03php 處理上百萬條的數(shù)據(jù)庫如何提高處理查詢速度
php 處理上百萬條的數(shù)據(jù)庫如何提高處理查詢速度2010-02-02php使用pdo連接并查詢sql數(shù)據(jù)庫的方法
這篇文章主要介紹了php使用pdo連接并查詢sql數(shù)據(jù)庫的方法,實例分析了常用的pdo連接方法與改進方法,并針對pdo技術進行了分析說明,需要的朋友可以參考下2014-12-12php array_chunk()函數(shù)用法與注意事項
這篇文章主要介紹了php array_chunk()函數(shù)用法與注意事項,結合實例形式分析了php數(shù)組分割函數(shù)array_chunk()相關功能、用法及操作注意事項,需要的朋友可以參考下2019-07-07