-
-
Notifications
You must be signed in to change notification settings - Fork 442
API QueryList
Jaeger edited this page Oct 9, 2017
·
9 revisions
Class QueryList
- Class name: QueryList
- Namespace: QL
Most of the methods in the QueryList can support both static and dynamic calls:
QueryList::html($html)->rules($rules)->range($range)->query();
QueryList::rules($rules)->html($html)->range($range)->query();
QueryList::range($range)->rules($rules)->html($html)->query();
mixed QL\QueryList::__construct()
QueryList constructor.
- Visibility: public
\QL\QueryList QL\QueryList::getInstance()
Get the QueryList instance
- Visibility: public
- This method is static.
$ql = QueryList::getInstance();
$data = $ql->get('http://www.baidu.com/s?wd=QueryList')->find('h3 a')->texts();
print_r($data->all());
null|\QL\Config QL\QueryList::config()
Get the Config instance
- Visibility: public
- This method is static.
QueryList::config()->use(My\MyPlugin::class,$arg1,$arg2,$arg3);
mixed QL\QueryList::destruct()
Destruction of resources
- Visibility: public
\QL\QueryList QL\QueryList::bind(string $name, \Closure $provide)
Bind a custom method to the QueryList object
- Visibility: public
- Example-1
$ql = QueryList::getInstance();
//Register a myHttp method into the QueryList object
$ql->bind('myHttp',function ($url){
$html = file_get_contents($url);
$this->setHtml($html);
return $this;
});
//And then can be registered by the name of the call
$data = $ql->myHttp('https://toutiao.io')->find('h3 a')->texts();
print_r($data->all());
//Or so use
$data = $ql->rules([
'title' => ['h3 a','text'],
'link' => ['h3 a','href']
])->myHttp('https://toutiao.io')->query()->getData();
print_r($data->all());
- Example-2
//Extend a picture download function
//param:$path Save the image locally for the image
$ql = QueryList::bind('downloadImage',function ($path){
$data = $this->getData()->map(function ($item) use($path){
//Get the picture
$img = file_get_contents($item['image']);
$localPath = $path.'/'.md5($img).'.jpg';
//Save the image to the local path
file_put_contents($localPath,$img);
//Add a custom local path field to the data array
$item['local_path'] = $localPath;
return $item;
});
//Update the data property
$this->setData($data);
return $this;
});
$data = $ql->get('http://desk.zol.com.cn')->rules([
'image' => ['#newPicList img','src']
])->query()->downloadImage('img')->getData();
print_r($data->all());
- Example-3
QueryList::bind('myHttp',function(){
return new MyHttp($this);
})
- Example-4
$ql = QueryList::bind('myHttp',function(){
return new MyHttp($this);
});
$ql->bind('other',function(){
//Use the previous bind
$this->myHttp();
return $this;
})
- $name string - <p>Invoking the name</p>
- $provide Closure - <p>Called method</p>
QL\QueryList::getHtml()
- Visibility: public
\QL\QueryList QL\Dom\Query::setHtml($html, null $charset)
- Visibility: public
- $html mixed
- $charset null
QL\QueryList::html($html, null $charset)
Set html,see setHtml()
.
- Visibility: public
- $html mixed
- $charset null
\QL\Dom\Elements QL\Dom\Query::find($selector)
Searches for all elements that match the specified expression.
- Visibility: public
- $selector mixed - A string containing a selector expression to match elements against.
\QL\QueryList QL\Dom\Query::rules(array $rules)
Set crawl rule
$rules = [
'rule_name1' => ['selector','HTML attribute | text | html','Tag filter list','callback'],
'rule_name2' => ['selector','HTML attribute | text | html','Tag filter list','callback'],
// ...
]
- Visibility: public
- $rules array
\QL\QueryList QL\Dom\Query::range($selector)
Set the slice area for crawl list
- Visibility: public
- $selector mixed
\QL\QueryList QL\Dom\Query::removeHead()
Remove HTML head,try to solve the garbled
- Visibility: public
\QL\QueryList QL\Dom\Query::query(\Closure|null $callback)
Execute the query rule
- Visibility: public
- $callback Closure|null
\Illuminate\Support\Collection|static QL\Dom\Query::getData(\Closure|null $callback)
Get crawl results
- Visibility: public
- $callback Closure|null
mixed QL\Dom\Query::setData(\Illuminate\Support\Collection $data)
- Visibility: public
- $data Illuminate\Support\Collection
QL\QueryList::encoding(string $outputEncoding,string $inputEncoding = null)
Encoding of html to solve the problem of distortion.
- Visibility: public
QL\QueryList::get($url,$args = null,$otherArgs = [])
Http get.see GuzzleHttp
- Visibility: public
- $url string
- $args array|string url params
- $otherArgs array GuzzleHttp options
QL\QueryList::post()
Http post.see GuzzleHttp
- Visibility: public
- $url string
- $args array post data
- $otherArgs array GuzzleHttp options
QL\QueryList::use($plugins,…$opt)
Install the plugin.
- Visibility: public
- $plugins string|array Plug-in class name, or it can be an array of plug-in class names
- …$opt mixed Multiple parameters that come with the plug-in.