欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php使用pthreads v3多線程實(shí)現(xiàn)抓取新浪新聞信息操作示例

 更新時(shí)間:2020年02月21日 08:47:31   作者:懷素真  
這篇文章主要介紹了php使用pthreads v3多線程實(shí)現(xiàn)抓取新浪新聞信息操作,結(jié)合實(shí)例形式分析了php使用pthreads多線程抓取新浪新聞信息具體實(shí)現(xiàn)步驟與操作技巧,需要的朋友可以參考下

本文實(shí)例講述了php使用pthreads v3多線程實(shí)現(xiàn)抓取新浪新聞信息。分享給大家供大家參考,具體如下:

我們使用pthreads,來寫一個(gè)多線程的抓取頁面小程序,把結(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 '時(shí)間',
 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)建連接對(duì)象
    self::$db = new PDO($this->dsn, $this->root, $this->pwd);

    //把require放到worker線程中,不要放到主線程中,不然會(huì)報(bào)錯(cuò)找不到類
    require './vendor/autoload.php';
  }

  //返回一個(gè)連接資源
  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)題,地址,時(shí)間
      $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) {
          //修改下時(shí)間,新浪的時(shí)間格式是這樣的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個(gè)分頁數(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下會(huì)有問題,由于each()已經(jīng)被廢棄,所以修改下源碼,each()全換成foreach()就好了。

運(yùn)行結(jié)果如下:

數(shù)據(jù)也保存進(jìn)了數(shù)據(jù)庫

當(dāng)然大家也可以再次通過url,拿到具體的頁面內(nèi)容,這里就不做演示了,有興趣的可以自已去實(shí)現(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ù)庫操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論