当前位置:首页 > PHP教程 > php高级应用 > 列表

php使用pthreads v3多线程实现抓取新浪新闻信息操作示例

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-13 12:49:04 浏览: 评论:0 

本文实例讲述了php使用pthreads v3多线程实现抓取新浪新闻信息,分享给大家供大家参考,具体如下:

我们使用pthreads,来写一个多线程的抓取页面小程序,把结果存到数据库里。

数据表结构如下:

  1. CREATE TABLE `tb_sina` ( 
  2.  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', 
  3.  `url` varchar(256) DEFAULT '' COMMENT 'url地址', 
  4.  `title` varchar(128) DEFAULT '' COMMENT '标题', 
  5.  `time` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '时间', 
  6.  PRIMARY KEY (`id`) 
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='sina新闻'; 

代码如下:

  1. <?php 
  2.  
  3. class DB extends Worker 
  4. { 
  5.   private static $db; 
  6.   private $dsn; 
  7.   private $root; 
  8.   private $pwd; 
  9.  
  10.   public function __construct($dsn, $root, $pwd) 
  11.   { 
  12.     $this->dsn = $dsn; 
  13.     $this->root = $root; 
  14.     $this->pwd = $pwd; 
  15.   } 
  16.  
  17.   public function run() 
  18.   { 
  19.     //创建连接对象 
  20.     self::$db = new PDO($this->dsn, $this->root, $this->pwd); 
  21.  
  22.     //把require放到worker线程中,不要放到主线程中,不然会报错找不到类 
  23.     require './vendor/autoload.php'; 
  24.   } 
  25.  
  26.   //返回一个连接资源 
  27.   public function getConn() 
  28.   { 
  29.     return self::$db; 
  30.   } 
  31. } 
  32.  
  33. class Sina extends Thread 
  34. { 
  35.   private $name; 
  36.   private $url; 
  37.  
  38.   public function __construct($name, $url) 
  39.   { 
  40.     $this->name = $name; 
  41.     $this->url = $url; 
  42.   } 
  43.  
  44.   public function run() 
  45.   { 
  46.     $db = $this->worker->getConn(); 
  47.  
  48.     if (emptyempty($db) || emptyempty($this->url)) { 
  49.       return false; 
  50.     } 
  51.  
  52.     $content = file_get_contents($this->url); 
  53.     if (!emptyempty($content)) { 
  54.       //获取标题,地址,时间 
  55.       $data = QL\QueryList::Query($content, [ 
  56.         'tit' => ['.c_tit > a', 'text'], 
  57.         'url' => ['.c_tit > a', 'href'], 
  58.         'time' => ['.c_time', 'text'], 
  59.       ], '', 'UTF-8', 'GB2312')->getData(); 
  60.  
  61.       //把获取的数据插入数据库 
  62.       if (!emptyempty($data)) { 
  63.         $sql = 'INSERT INTO tb_sina(`url`, `title`, `time`) VALUES'; 
  64.         foreach ($data as $row) { 
  65.           //修改下时间,新浪的时间格式是这样的04-23 15:30 
  66.           $time = date('Y') . '-' . $row['time'] . ':00'; 
  67.           $sql .= "('{$row['url']}', '{$row['tit']}', '{$time}'),"; 
  68.         } 
  69.         $sql = rtrim($sql, ','); 
  70.         $ret = $db->exec($sql); 
  71.  
  72.         if ($ret !== false) { 
  73.           echo "线程{$this->name}成功插入{$ret}条数据\n"; 
  74.         } else { 
  75.           var_dump($db->errorInfo()); 
  76.         } 
  77.       } 
  78.     } 
  79.   } 
  80. } 
  81.  
  82. //抓取页面地址 
  83. $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='; 
  84. //创建pool池 
  85. $pool = new Pool(5, 'DB', ['mysql:dbname=test;host=192.168.33.226', 'root', '']); 
  86.  
  87. //获取100个分页数据 
  88. for ($ix = 1; $ix <= 100; $ix++) { 
  89.   $pool->submit(new Sina($ix, $url . $ix)); 
  90. } 
  91.  
  92. //循环收集垃圾,阻塞主线程,等待子线程结束 
  93. while ($pool->collect()) ; 
  94. $pool->shutdown(); 

由于使用到了QueryList,大家可以通过composer进行安装。

composer require jaeger/querylist

不过安装的版本是3.2,在我的php7.2下会有问题,由于each()已经被废弃,所以修改下源码,each()全换成foreach()就好了。

运行结果如下:

php使用pthreads v3多线程实现抓取新浪新闻信息操作示例

数据也保存进了数据库

php使用pthreads v3多线程实现抓取新浪新闻信息操作示例

当然大家也可以再次通过url,拿到具体的页面内容,这里就不做演示了,有兴趣的可以自已去实现。

Tags: pthreads php多线程

分享到: