QueryList is a simple, elegant, extensible PHP Web Scraper (crawler/spider) ,based on phpQuery.

API Documentation

Chinese document

Features

  • Have the same CSS3 DOM selector as jQuery
  • Have the same DOM manipulation API as jQuery
  • Have a generic list crawling program
  • Have a strong HTTP request suite, easy to achieve such as: simulated landing, forged browser, HTTP proxy and other complex network requests
  • Have a messy code solution
  • Have powerful content filtering, you can use the jQuey selector to filter content
  • Has a high degree of modular design, scalability and strong
  • Have an expressive API
  • Has a wealth of plug-ins

Through plug-ins you can easily implement things like:

  • Multithreaded crawl
  • Crawl JavaScript dynamic rendering page (PhantomJS/headless WebKit)
  • Image downloads to local
  • Simulate browser behavior such as submitting Form forms
  • Web crawler
  • .

Installation

By Composer installation:

composer require jaeger/querylist
Copy the code

DOM Traversal and Manipulation

  • Crawl “GitHub” all Picture Links
QueryList: :get('https://github.com')->find('img')->attrs('src');Copy the code

  • Crawl Google search results
$ql = QueryList: :get('https://www.google.co.jp/search?q=QueryList');

$ql->find('title')->text(); //The page title
$ql->find('meta[name=keywords]')->content; //The page keywords

$ql->find('h3>a')->texts(); //Get a list of search results titles
$ql->find('h3>a')->attrs('href'); //Get a list of search results links

$ql->find('img')->src; //Gets the link address of the first image
$ql->find('img:eq(1)')->src; //Gets the link address of the second image
$ql->find('img')->eq(2)->src; //Gets the link address of the third image
// Loop all the images
$ql->find('img')->map(function($img) {
	echo $img->alt;  //Print the alt attribute of the image
});Copy the code

  • More usage
$ql->find('#head')->append('<div>Append content</div>')->find('div')->htmls();
$ql->find('.two')->children('img')->attrs('alt'); // Get the class is the "two" element under all img child nodes
// Loop class is the "two" element under all child nodes
$data = $ql->find('.two')->children()->map(function ($item) {
    // Use "is" to determine the node type
    if($item->is('a')) {
        return $item->text();
    }elseif($item->is('img'))
    {
        return $item->alt;
    }
});

$ql->find('a')->attr('href'.'newVal')->removeClass('className')->html('newHtml')->.
$ql->find('div > p')->add('div > ul')->filter(':has(a)')->find('p:first')->nextAll()->andSelf()->.
$ql->find('div.old')->replaceWith( $ql->find('div.new')->clone())->appendTo('.trash')->prepend('Deleted')->.Copy the code

Encode convert

// Out charset :UTF-8
// In charset :GB2312
QueryList: :get('https://top.etao.com')->encoding('UTF-8'.'GB2312')->find('a')->texts();

// Out charset:UTF-8
// In charset:Automatic Identification
QueryList: :get('https://top.etao.com')->encoding('UTF-8')->find('a')->texts();Copy the code

Submit forms

Login GitHub

// Get the QueryList instance
$ql = QueryList: :getInstance();
// Get the login form
$form = $ql->get('https://github.com/login')->find('form');

// Fill in the GitHub username and password
$form->find('input[name=login]')->val('your github username or email');
$form->find('input[name=password]')->val('your github password');

// Serialize the form data
$fromData = $form->serializeArray();
$postData = [];
foreach ($fromData as $item) {
    $postData[$item['name']] = $item['value'];
}

// Submit the login form
$actionUrl = 'https://github.com'.$form->attr('action');
$ql->post($actionUrl.$postData);
// To determine whether the login is successful
// echo $ql->getHtml();
$userName = $ql->find('.header-nav-current-user>.css-truncate-target')->text();
if($userName)
{
    echo 'Login successful ! Welcome:'.$userName;
}else{
    echo 'Login failed !';
}Copy the code

Plugin used

  • Use the PhantomJS plugin to crawl JavaScript dynamically rendered pages:
// Set the PhantomJS binary file path during installation
$ql = QueryList: :use(PhantomJs: :class.'/usr/local/bin/phantomjs');

//Crawl "500px" all Picture Links
$data = $ql->browser('https://500px.com/editors')->find('img')->attrs('src');
print_r($data->all());

// Use the HTTP proxy
$ql->browser('https://500px.com/editors'.false[
	'--proxy' = > '192.168.1.42:8080'.
    '--proxy-type' = > 'http'
])Copy the code

  • Using the CURL multithreading plug-in, multi-threaded crawling GitHub trending :
$ql = QueryList: :use(CurlMulti: :class);
$ql->curlMulti([
    'https://github.com/trending/php'.
    'https://github.com/trending/go'.
    //. more urls
])
 // Called if task is success
 ->success(function (QueryList $ql.CurlMulti $curl.$r) {
    echo "Current url:{$r['info'] ['url']} \r\n";
    $data = $ql->find('h3 a')->texts();
    print_r($data->all());
})
 // Task fail callback
->error(function ($errorInfo.CurlMulti $curl) {
    echo "Current url:{$errorInfo['info'] ['url']} \r\n";
    print_r($errorInfo['error']);
})
->start([
	// Maximum number of threads
    'maxThread' = > 10.
    // Number of error retries
    'maxTry' = > 3.
]);
Copy the code

Contributing

Welcome to contribute code for the QueryList. About Contributing Plugins can be viewed:QueryList Plugin Contributing Guide

[email protected]

If this library is useful for you, say thanks buying me a beer !

Lisence

QueryList is licensed under the license of MIT. See the LICENSE for more details.