Jaeger преди 9 години
родител
ревизия
ab08c88f12
променени са 2 файла, в които са добавени 207 реда и са изтрити 1 реда
  1. 104 0
      Baidu.php
  2. 103 1
      README.md

+ 104 - 0
Baidu.php

@@ -0,0 +1,104 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Jaeger <JaegerCode@gmail.com>
+ * Date: 2017/10/1
+ * Baidu searcher
+ */
+
+namespace QL\Ext;
+
+use QL\Contracts\PluginContract;
+use QL\QueryList;
+
+class Baidu implements PluginContract
+{
+    protected $ql;
+    protected $keyword;
+    protected $pageNumber = 10;
+    const API = 'https://www.baidu.com/s';
+    const RULES = [
+      'title' => ['h3','text'],
+      'link' => ['h3>a','href']
+    ];
+    const RANGE = '.result';
+
+    public function __construct(QueryList $ql, $pageNumber)
+    {
+        $this->ql = $ql->rules(self::RULES)->range(self::RANGE);
+        $this->pageNumber = $pageNumber;
+    }
+
+    public static function install(QueryList $queryList, ...$opt)
+    {
+        $name = $opt[0] ?? 'baidu';
+        $queryList->bind($name,function ($pageNumber = 10){
+            return new Baidu($this,$pageNumber);
+        });
+    }
+
+    public function search($keyword)
+    {
+        $this->keyword = $keyword;
+        return $this;
+    }
+
+    public function page($page = 1,$realURL = false)
+    {
+        return $this->query($page)->query()->getData(function ($item) use($realURL){
+            $realURL && $item['link'] = $this->getRealURL($item['link']);
+            return $item;
+        });
+    }
+
+    public function getCount()
+    {
+        $count = 0;
+        $text =  $this->query(1)->find('.nums')->text();
+        if(preg_match('/[\d,]+/',$text,$arr))
+        {
+            $count = str_replace(',','',$arr[0]);
+        }
+        return (int)$count;
+    }
+
+    public function getCountPage()
+    {
+        $count = $this->getCount();
+        $countPage = ceil($count / $this->pageNumber);
+        return $countPage;
+    }
+
+    protected function query($page = 1)
+    {
+        $this->ql->get(self::API,[
+            'wd' => $this->keyword,
+            'rn' => $this->pageNumber,
+            'pn' => $this->pageNumber * ($page-1)
+        ]);
+        return $this->ql;
+    }
+
+    protected  function getRealURL($url)
+    {
+        //得到百度跳转的真正地址
+        $header = get_headers($url,1);
+        if (strpos($header[0],'301') || strpos($header[0],'302'))
+        {
+            if(is_array($header['Location']))
+            {
+                //return $header['Location'][count($header['Location'])-1];
+                return $header['Location'][0];
+            }
+            else
+            {
+                return $header['Location'];
+            }
+        }
+        else
+        {
+            return $url;
+        }
+    }
+
+}

+ 103 - 1
README.md

@@ -1,2 +1,104 @@
 # QueryList-Rule-Baidu
-QueryList Plugin: Baidu searcher. QueryList百度搜索插件
+QueryList Plugin: Baidu searcher. 
+
+QueryList插件:百度搜索引擎
+
+## Installation for QueryList4
+```
+composer require jaeger/querylist-rule-baidu
+```
+
+## API
+- Baidu **baidu($pageNumber = 10)**:get Baidu Searcher.
+
+class **Baidu**:
+- Baidu **search($keyword)**:set search keyword.
+- int **getCount()**:Get the total number of search results.
+- int **getCountPage()**:Get the total number of pages.
+- Collection **page($page = 1,$realURL = false)**:Get search results
+
+## Usage
+- Installation Plugin
+
+```php
+use QL\QueryList;
+use QL\Ext\Baidu;
+
+$ql = QueryList::getInstance();
+$ql->use(Baidu::class);
+//or Custom function name
+$ql->use(Baidu::class,'baidu');
+```
+- Example-1
+
+```
+$baidu = $ql->baidu(10)
+$searcher = $baidu->search('QueryList');
+$count = $searcher->getCount();
+$data = $searcher->page(1);
+$data = $searcher->page(2);
+
+$searcher = $baidu->search('php');
+$countPage = $searcher->getCountPage();
+for ($page = 1; $page <= $countPage; $page++)
+{
+    $data = $searcher->page($page);
+}
+```
+
+- Example-2
+
+```
+$baidu = $ql->baidu(3)
+$searcher = $baidu->search('QueryList');
+
+$data = $searcher->page(1);
+print_r($data->all());
+
+// Get real url
+$data = $searcher->page(1,true);
+print_r($data->all());
+```
+Out:
+
+```
+Array
+(
+    [0] => Array
+        (
+            [title] => QueryList|基于phpQuery的无比强大的PHP采集工具
+            [link] => http://www.baidu.com/link?url=qRAXrUIcrxuLQ4Pn_rL25HvpDwugxgLkmwB74wTBuLflWaDTNY1d27gdxMwddbfn
+        )
+    [1] => Array
+        (
+            [title] => 介绍- QueryList指导文档
+            [link] => http://www.baidu.com/link?url=NgoB517LCcb7tt37_x74uF0N-8pfhSemhA5qoB0SHf8HY9P_MwKbN80nf9zvd3V5
+        )
+    [2] => Array
+        (
+            [title] => PHP 用QueryList抓取网页内容 - wb145230 - 博客园
+            [link] => http://www.baidu.com/link?url=kDkpY9eZ6CsiT1SWomRWEYPauHseHn2FseSdPnsOoulWCkD3DK6QMT75urFGHLyeG_M9yTD0BCm-s5jGQRi_S_
+        )
+
+)
+
+Array
+(
+    [0] => Array
+        (
+            [title] => QueryList|基于phpQuery的无比强大的PHP采集工具
+            [link] => http://www.querylist.cc/
+        )
+    [1] => Array
+        (
+            [title] => 介绍- QueryList指导文档
+            [link] => http://doc.querylist.cc/
+        )
+    [2] => Array
+        (
+            [title] => PHP 用QueryList抓取网页内容 - wb145230 - 博客园
+            [link] => http://www.cnblogs.com/wb145230/p/4716403.html
+        )
+)
+
+```