Skip to content

API QueryList

Jaeger edited this page Oct 9, 2017 · 9 revisions

QL\QueryList

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();

Methods

__construct

mixed QL\QueryList::__construct()

QueryList constructor.

  • Visibility: public

getInstance

\QL\QueryList QL\QueryList::getInstance()

Get the QueryList instance

  • Visibility: public
  • This method is static.
Examples
$ql = QueryList::getInstance();
$data = $ql->get('http://www.baidu.com/s?wd=QueryList')->find('h3 a')->texts();
print_r($data->all());

config

null|\QL\Config QL\QueryList::config()

Get the Config instance

  • Visibility: public
  • This method is static.
Examples
QueryList::config()->use(My\MyPlugin::class,$arg1,$arg2,$arg3);

destruct

mixed QL\QueryList::destruct()

Destruction of resources

  • Visibility: public

bind

\QL\QueryList QL\QueryList::bind(string $name, \Closure $provide)

Bind a custom method to the QueryList object

  • Visibility: public
Examples
  • 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;
})
Arguments
  • $name string - <p>Invoking the name</p>
  • $provide Closure - <p>Called method</p>

getHtml

 QL\QueryList::getHtml()
  • Visibility: public

setHtml

\QL\QueryList QL\Dom\Query::setHtml($html, null $charset)
  • Visibility: public
Arguments
  • $html mixed
  • $charset null

html

 QL\QueryList::html($html, null $charset)

Set html,see setHtml().

  • Visibility: public
Arguments
  • $html mixed
  • $charset null

find

\QL\Dom\Elements QL\Dom\Query::find($selector)

Searches for all elements that match the specified expression.

  • Visibility: public
Arguments
  • $selector mixed - A string containing a selector expression to match elements against.

rules

\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
Arguments
  • $rules array

range

\QL\QueryList QL\Dom\Query::range($selector)

Set the slice area for crawl list

  • Visibility: public
Arguments
  • $selector mixed

removeHead

\QL\QueryList QL\Dom\Query::removeHead()

Remove HTML head,try to solve the garbled

  • Visibility: public

query

\QL\QueryList QL\Dom\Query::query(\Closure|null $callback)

Execute the query rule

  • Visibility: public
Arguments
  • $callback Closure|null

getData

\Illuminate\Support\Collection|static QL\Dom\Query::getData(\Closure|null $callback)

Get crawl results

  • Visibility: public
Arguments
  • $callback Closure|null

setData

mixed QL\Dom\Query::setData(\Illuminate\Support\Collection $data)
  • Visibility: public
Arguments
  • $data Illuminate\Support\Collection

encoding

 QL\QueryList::encoding(string $outputEncoding,string $inputEncoding = null)

Encoding of html to solve the problem of distortion.

  • Visibility: public

get

 QL\QueryList::get($url,$args = null,$otherArgs = [])

Http get.see GuzzleHttp

  • Visibility: public
Arguments
  • $url string
  • $args array|string url params
  • $otherArgs array GuzzleHttp options

post

 QL\QueryList::post()

Http post.see GuzzleHttp

  • Visibility: public
Arguments
  • $url string
  • $args array post data
  • $otherArgs array GuzzleHttp options

use

 QL\QueryList::use($plugins,…$opt)

Install the plugin.

  • Visibility: public
Arguments
  • $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.
Clone this wiki locally