|
@@ -13,6 +13,7 @@ use QL\QueryList;
|
|
|
|
|
|
|
|
class Baidu implements PluginContract
|
|
class Baidu implements PluginContract
|
|
|
{
|
|
{
|
|
|
|
|
+ private $is_pc = true;
|
|
|
protected $ql;
|
|
protected $ql;
|
|
|
protected $keyword;
|
|
protected $keyword;
|
|
|
protected $pageNumber = 10;
|
|
protected $pageNumber = 10;
|
|
@@ -22,24 +23,34 @@ class Baidu implements PluginContract
|
|
|
'Accept-Encoding' => 'gzip, deflate, br',
|
|
'Accept-Encoding' => 'gzip, deflate, br',
|
|
|
]
|
|
]
|
|
|
];
|
|
];
|
|
|
- const API = 'https://www.baidu.com/s';
|
|
|
|
|
|
|
+ const API = [
|
|
|
|
|
+ 'PC' => 'https://www.baidu.com/s',
|
|
|
|
|
+ 'M' => 'https://www.baidu.com/s',
|
|
|
|
|
+ ];
|
|
|
const RULES = [
|
|
const RULES = [
|
|
|
- 'title' => ['h3','text'],
|
|
|
|
|
- 'link' => ['h3>a','href']
|
|
|
|
|
|
|
+ 'title' => ['h3', 'text'],
|
|
|
|
|
+ 'link' => ['h3>a', 'href']
|
|
|
];
|
|
];
|
|
|
const RANGE = '.result';
|
|
const RANGE = '.result';
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @var string
|
|
|
|
|
+ */
|
|
|
|
|
+ private $api;
|
|
|
|
|
|
|
|
- public function __construct(QueryList $ql, $pageNumber)
|
|
|
|
|
|
|
+ public function __construct(QueryList $ql, $pageNumber, $api)
|
|
|
{
|
|
{
|
|
|
$this->ql = $ql->rules(self::RULES)->range(self::RANGE);
|
|
$this->ql = $ql->rules(self::RULES)->range(self::RANGE);
|
|
|
$this->pageNumber = $pageNumber;
|
|
$this->pageNumber = $pageNumber;
|
|
|
|
|
+ $this->api = self::API[$api];
|
|
|
|
|
+ $this->is_pc = $api == "PC";
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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){
|
|
|
|
|
- return new Baidu($this,$pageNumber);
|
|
|
|
|
|
|
+ $queryList->bind($name, function ($pageNumber = 10, $api = 'PC') {
|
|
|
|
|
+ return new Baidu($this, $pageNumber, $api);
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -55,9 +66,9 @@ class Baidu implements PluginContract
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- 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']);
|
|
$realURL && $item['link'] = $this->getRealURL($item['link']);
|
|
|
return $item;
|
|
return $item;
|
|
|
});
|
|
});
|
|
@@ -66,10 +77,9 @@ class Baidu implements PluginContract
|
|
|
public function getCount()
|
|
public function getCount()
|
|
|
{
|
|
{
|
|
|
$count = 0;
|
|
$count = 0;
|
|
|
- $text = $this->query(1)->find('.nums')->text();
|
|
|
|
|
- if(preg_match('/[\d,]+/',$text,$arr))
|
|
|
|
|
- {
|
|
|
|
|
- $count = str_replace(',','',$arr[0]);
|
|
|
|
|
|
|
+ $text = $this->query(1)->find('.nums')->text();
|
|
|
|
|
+ if (preg_match('/[\d,]+/', $text, $arr)) {
|
|
|
|
|
+ $count = str_replace(',', '', $arr[0]);
|
|
|
}
|
|
}
|
|
|
return (int)$count;
|
|
return (int)$count;
|
|
|
}
|
|
}
|
|
@@ -83,11 +93,11 @@ class Baidu implements PluginContract
|
|
|
|
|
|
|
|
protected function query($page = 1)
|
|
protected function query($page = 1)
|
|
|
{
|
|
{
|
|
|
- $this->ql->get(self::API,[
|
|
|
|
|
|
|
+ $this->ql->get($this->api, [
|
|
|
'wd' => $this->keyword,
|
|
'wd' => $this->keyword,
|
|
|
'rn' => $this->pageNumber,
|
|
'rn' => $this->pageNumber,
|
|
|
- 'pn' => $this->pageNumber * ($page-1)
|
|
|
|
|
- ],$this->httpOpt);
|
|
|
|
|
|
|
+ 'pn' => $this->pageNumber * ($page - 1)
|
|
|
|
|
+ ], $this->httpOpt);
|
|
|
return $this->ql;
|
|
return $this->ql;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -96,24 +106,18 @@ class Baidu implements PluginContract
|
|
|
* @param $url
|
|
* @param $url
|
|
|
* @return mixed
|
|
* @return mixed
|
|
|
*/
|
|
*/
|
|
|
- protected function getRealURL($url)
|
|
|
|
|
|
|
+ protected function getRealURL($url)
|
|
|
{
|
|
{
|
|
|
- if(empty($url)) return $url;
|
|
|
|
|
- $header = get_headers($url,1);
|
|
|
|
|
- if (strpos($header[0],'301') || strpos($header[0],'302'))
|
|
|
|
|
- {
|
|
|
|
|
- if(is_array($header['Location']))
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (empty($url)) return $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'][count($header['Location'])-1];
|
|
|
return $header['Location'][0];
|
|
return $header['Location'][0];
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } else {
|
|
|
return $header['Location'];
|
|
return $header['Location'];
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } else {
|
|
|
return $url;
|
|
return $url;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|