陈德本 5 سال پیش
والد
کامیت
60942609fb
1فایلهای تغییر یافته به همراه47 افزوده شده و 16 حذف شده
  1. 47 16
      Baidu.php

+ 47 - 16
Baidu.php

@@ -13,25 +13,38 @@ use QL\QueryList;
 
 
 class Baidu implements PluginContract
 class Baidu implements PluginContract
 {
 {
-    private $is_pc = true;
+    private $is_pc;
     protected $ql;
     protected $ql;
     protected $keyword;
     protected $keyword;
     protected $pageNumber = 10;
     protected $pageNumber = 10;
     protected $httpOpt = [
     protected $httpOpt = [
-        'headers' => [
+        'PC'=>[ 'headers' => [
             'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
             'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
             'Accept-Encoding' => 'gzip, deflate, br',
             'Accept-Encoding' => 'gzip, deflate, br',
-        ]
+        ]],
+        'M'=>[ 'headers' => [
+            'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
+            'Accept-Encoding' => 'gzip, deflate, br',
+        ]],
     ];
     ];
     const API = [
     const API = [
         'PC' => 'https://www.baidu.com/s',
         'PC' => 'https://www.baidu.com/s',
-        'M' => 'https://www.baidu.com/s',
+        'M' => 'https://m.baidu.com/s',
     ];
     ];
     const RULES = [
     const RULES = [
-        'title' => ['h3', 'text'],
-        'link' => ['h3>a', 'href']
+        'PC' => [
+            'title' => ['h3', 'text'],
+            'link' => ['h3>a', 'href']
+        ],
+        'M' => [
+            'title' => ['h3', 'text'],
+            'link' => ['', 'data-log']
+        ],
+    ];
+    const RANGE = [
+        'PC' => '.result',
+        'M' => '.result'
     ];
     ];
-    const RANGE = '.result';
     /**
     /**
      * @var string
      * @var string
      */
      */
@@ -39,17 +52,19 @@ class Baidu implements PluginContract
 
 
     public function __construct(QueryList $ql, $pageNumber, $api)
     public function __construct(QueryList $ql, $pageNumber, $api)
     {
     {
-        $this->ql = $ql->rules(self::RULES)->range(self::RANGE);
+        $this->ql = $ql->rules(self::RULES[$api])->range(self::RANGE[$api]);
         $this->pageNumber = $pageNumber;
         $this->pageNumber = $pageNumber;
         $this->api = self::API[$api];
         $this->api = self::API[$api];
         $this->is_pc = $api == "PC";
         $this->is_pc = $api == "PC";
+        $this->httpOpt=$this->httpOpt[$api];
 
 
     }
     }
 
 
     public static function install(QueryList $queryList, ...$opt)
     public static function install(QueryList $queryList, ...$opt)
     {
     {
         $name = $opt[0] ?? 'baidu';
         $name = $opt[0] ?? 'baidu';
-        $queryList->bind($name, function ($pageNumber = 10, $api = 'PC') {
+        $api = $opt[1] ?? 'PC';
+        $queryList->bind($name, function ($pageNumber = 10) use ($api) {
             return new Baidu($this, $pageNumber, $api);
             return new Baidu($this, $pageNumber, $api);
         });
         });
     }
     }
@@ -69,13 +84,20 @@ class Baidu implements PluginContract
     public function page($page = 1, $realURL = false)
     public function page($page = 1, $realURL = false)
     {
     {
         return $this->query($page)->query()->getData(function ($item) use ($realURL) {
         return $this->query($page)->query()->getData(function ($item) use ($realURL) {
-            $realURL && $item['link'] = $this->getRealURL($item['link']);
+            if (!$this->is_pc) {
+                $item['link'] = json_decode($item['link'], true)['mu'];
+            } else {
+                $realURL && $item['link'] = $this->getRealURL($item['link']);
+            }
             return $item;
             return $item;
         });
         });
     }
     }
 
 
     public function getCount()
     public function getCount()
     {
     {
+        if (!$this->is_pc) {
+            return 1000;
+        }
         $count = 0;
         $count = 0;
         $text = $this->query(1)->find('.nums')->text();
         $text = $this->query(1)->find('.nums')->text();
         if (preg_match('/[\d,]+/', $text, $arr)) {
         if (preg_match('/[\d,]+/', $text, $arr)) {
@@ -87,17 +109,26 @@ class Baidu implements PluginContract
     public function getCountPage()
     public function getCountPage()
     {
     {
         $count = $this->getCount();
         $count = $this->getCount();
-        $countPage = ceil($count / $this->pageNumber);
+        $countPage = $this->ceil($count / $this->pageNumber);
         return $countPage;
         return $countPage;
     }
     }
 
 
     protected function query($page = 1)
     protected function query($page = 1)
     {
     {
-        $this->ql->get($this->api, [
-            'wd' => $this->keyword,
-            'rn' => $this->pageNumber,
-            'pn' => $this->pageNumber * ($page - 1)
-        ], $this->httpOpt);
+        if ($this->is_pc) {
+            $this->ql->get($this->api, [
+                'wd' => $this->keyword,
+                'rn' => $this->pageNumber,
+                'pn' => $this->pageNumber * ($page - 1)
+            ], $this->httpOpt);
+        } else {
+            $this->ql->get($this->api, [
+                'word' => $this->keyword,
+                'usm' => $this->pageNumber,
+                'pn' => $this->pageNumber * ($page - 1)
+            ], $this->httpOpt);
+        }
+
         return $this->ql;
         return $this->ql;
     }
     }