QueryList/README.md

304 lines
9.1 KiB
Markdown
Raw Normal View History

2017-09-30 12:04:27 +08:00
<p align="center">
<img width="150" src="logo.png" alt="QueryList">
<br>
<br>
</p>
2017-09-30 12:02:25 +08:00
2017-10-01 12:49:01 +08:00
# QueryList
2017-10-02 10:30:24 +08:00
`QueryList` is a simple, elegant, extensible PHP Web Scraper (crawler/spider) ,based on phpQuery.
2017-10-01 12:49:01 +08:00
2017-10-09 11:27:08 +08:00
[API Documentation](https://github.com/jae-jae/QueryList/wiki)
2017-10-09 02:44:07 +08:00
2017-10-01 12:49:01 +08:00
[中文文档](README-ZH.md)
## 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
2017-09-29 00:36:02 +08:00
- .....
2017-10-01 12:49:01 +08:00
## Requirements
2017-09-29 00:36:02 +08:00
- PHP >= 7.0
2017-10-01 12:49:01 +08:00
## Installation
By Composer installation:
2017-09-29 00:36:02 +08:00
```
2017-09-29 18:43:24 +08:00
composer require jaeger/querylist
2017-09-29 00:36:02 +08:00
```
2017-10-01 12:49:01 +08:00
## Usage
2017-09-29 12:16:50 +08:00
2017-10-01 12:49:01 +08:00
#### DOM Traversal and Manipulation
- Crawl「GitHub」all picture links
2017-09-29 12:16:50 +08:00
```php
2017-10-01 12:49:01 +08:00
QueryList::get('https://github.com')->find('img')->attrs('src');
2017-09-29 12:16:50 +08:00
```
2017-10-01 12:49:01 +08:00
- Crawl Google search results
2017-09-29 12:16:50 +08:00
2017-09-29 12:18:23 +08:00
```php
2017-10-01 12:49:01 +08:00
$ql = QueryList::get('https://www.google.co.jp/search?q=QueryList');
2017-09-29 12:16:50 +08:00
2017-10-01 12:49:01 +08:00
$ql->find('title')->text(); //The page title
$ql->find('meta[name=keywords]')->content; //The page keywords
2017-09-29 12:16:50 +08:00
2017-10-01 12:49:01 +08:00
$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
2017-09-29 12:16:50 +08:00
2017-10-01 12:49:01 +08:00
$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
2017-09-29 12:16:50 +08:00
$ql->find('img')->map(function($img){
2017-10-01 12:49:01 +08:00
echo $img->alt; //Print the alt attribute of the image
2017-09-29 12:16:50 +08:00
});
```
2017-10-01 12:49:01 +08:00
- More usage
2017-09-29 12:16:50 +08:00
2017-09-29 12:18:23 +08:00
```php
2017-10-01 12:49:01 +08:00
$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
2017-09-29 12:16:50 +08:00
$data = $ql->find('.two')->children()->map(function ($item){
2017-10-01 12:49:01 +08:00
// Use "is" to determine the node type
2017-09-29 12:16:50 +08:00
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()->...
2017-09-29 14:59:16 +08:00
$ql->find('div.old')->replaceWith( $ql->find('div.new')->clone())->appendTo('.trash')->prepend('Deleted')->...
2017-09-29 12:16:50 +08:00
```
2017-10-01 12:49:01 +08:00
#### List crawl
Crawl the title and link of the Google search results list:
2017-09-29 12:18:23 +08:00
```php
2017-10-01 12:49:01 +08:00
$data = QueryList::get('https://www.google.co.jp/search?q=QueryList')
// Set the crawl rules
2017-09-29 12:16:50 +08:00
->rules([
'title'=>array('h3','text'),
'link'=>array('h3>a','href')
])
->query()->getData();
print_r($data->all());
```
2017-10-01 12:49:01 +08:00
Results:
2017-09-29 12:16:50 +08:00
```
Array
(
[0] => Array
(
2017-10-01 12:49:01 +08:00
[title] => Angular - QueryList
[link] => https://angular.io/api/core/QueryList
2017-09-29 12:16:50 +08:00
)
[1] => Array
(
2017-10-01 12:49:01 +08:00
[title] => QueryList | @angular/core - Angularリファレンス - Web Creative Park
[link] => http://www.webcreativepark.net/angular/querylist/
2017-09-29 12:16:50 +08:00
)
[2] => Array
(
2017-10-01 12:49:01 +08:00
[title] => QueryListにQueryを追加したり、追加されたことを感知する | TIPS ...
[link] => http://www.webcreativepark.net/angular/querylist_query_add_subscribe/
2017-09-29 12:16:50 +08:00
)
//...
)
```
2017-10-01 12:49:01 +08:00
#### Encode convert
2017-09-29 15:05:33 +08:00
```php
2017-10-01 12:49:01 +08:00
// Out charset :UTF-8
// In charset :GB2312
2017-09-29 15:05:33 +08:00
QueryList::get('https://top.etao.com')->encoding('UTF-8','GB2312')->find('a')->texts();
2017-10-01 12:49:01 +08:00
// Out charset:UTF-8
// In charset:Automatic Identification
2017-09-29 15:05:33 +08:00
QueryList::get('https://top.etao.com')->encoding('UTF-8')->find('a')->texts();
```
2017-09-29 12:16:50 +08:00
2017-10-08 22:48:06 +08:00
#### HTTP Client (GuzzleHttp)
2017-10-01 12:49:01 +08:00
- Carry cookie login GitHub
2017-09-29 15:05:33 +08:00
```php
2017-10-01 12:49:01 +08:00
//Crawl GitHub content
$ql = QueryList::get('https://github.com','param1=testvalue & params2=somevalue',[
'headers' => [
// Fill in the cookie from the browser
'Cookie' => 'SINAGLOBAL=546064; wb_cmtLike_2112031=1; wvr=6;....'
]
2017-09-29 14:59:16 +08:00
]);
//echo $ql->getHtml();
2017-10-01 12:49:01 +08:00
$userName = $ql->find('.header-nav-current-user>.css-truncate-target')->text();
echo $userName;
2017-09-29 14:59:16 +08:00
```
2017-10-01 12:49:01 +08:00
- Use the Http proxy
2017-09-29 15:05:33 +08:00
```php
2017-09-29 14:59:16 +08:00
$urlParams = ['param1' => 'testvalue','params2' => 'somevalue'];
$opts = [
2017-10-01 12:49:01 +08:00
// Set the http proxy
2017-09-29 14:59:16 +08:00
'proxy' => 'http://222.141.11.17:8118',
2017-10-01 12:49:01 +08:00
//Set the timeout time in seconds
2017-09-29 14:59:16 +08:00
'timeout' => 30,
2017-10-01 12:49:01 +08:00
// Fake HTTP headers
2017-09-29 14:59:16 +08:00
'headers' => [
'Referer' => 'https://querylist.cc/',
'User-Agent' => 'testing/1.0',
'Accept' => 'application/json',
'X-Foo' => ['Bar', 'Baz'],
'Cookie' => 'abc=111;xxx=222'
]
];
$ql->get('http://httpbin.org/get',$urlParams,$opts);
// echo $ql->getHtml();
```
2017-10-01 12:49:01 +08:00
- Analog login
2017-09-29 15:05:33 +08:00
```php
2017-10-01 12:49:01 +08:00
// Post login
2017-09-29 14:59:16 +08:00
$ql = QueryList::post('http://xxxx.com/login',[
'username' => 'admin',
'password' => '123456'
])->get('http://xxx.com/admin');
2017-10-01 12:49:01 +08:00
// Crawl pages that need to be logged in to access
2017-09-29 14:59:16 +08:00
$ql->get('http://xxx.com/admin/page');
//echo $ql->getHtml();
```
2017-09-29 12:16:50 +08:00
2017-10-01 12:49:01 +08:00
#### Submit forms
Login GitHub
2017-09-29 15:05:33 +08:00
```php
2017-10-01 12:49:01 +08:00
// Get the QueryList instance
2017-09-29 14:59:16 +08:00
$ql = QueryList::getInstance();
2017-10-01 12:49:01 +08:00
// Get the login form
2017-09-29 14:59:16 +08:00
$form = $ql->get('https://github.com/login')->find('form');
2017-10-01 12:49:01 +08:00
// Fill in the GitHub username and password
2017-09-29 14:59:16 +08:00
$form->find('input[name=login]')->val('your github username or email');
$form->find('input[name=password]')->val('your github password');
2017-10-01 12:49:01 +08:00
// Serialize the form data
2017-09-29 14:59:16 +08:00
$fromData = $form->serializeArray();
$postData = [];
foreach ($fromData as $item) {
$postData[$item['name']] = $item['value'];
}
2017-10-01 12:49:01 +08:00
// Submit the login form
2017-09-29 14:59:16 +08:00
$actionUrl = 'https://github.com'.$form->attr('action');
$ql->post($actionUrl,$postData);
2017-10-01 12:49:01 +08:00
// To determine whether the login is successful
2017-09-29 14:59:16 +08:00
// echo $ql->getHtml();
$userName = $ql->find('.header-nav-current-user>.css-truncate-target')->text();
if($userName)
{
2017-10-01 12:49:01 +08:00
echo 'Login successful ! Welcome:'.$userName;
2017-09-29 14:59:16 +08:00
}else{
2017-10-01 12:49:01 +08:00
echo 'Login failed !';
2017-09-29 14:59:16 +08:00
}
```
2017-10-01 12:49:01 +08:00
#### Bind function extension
Customize the extension of a `myHttp` method:
2017-09-29 14:59:16 +08:00
```php
$ql = QueryList::getInstance();
2017-10-01 12:49:01 +08:00
//Bind a `myHttp` method to the QueryList object
2017-09-29 14:59:16 +08:00
$ql->bind('myHttp',function ($url){
2017-10-08 22:48:06 +08:00
// $this is the current QueryList object
2017-09-29 14:59:16 +08:00
$html = file_get_contents($url);
$this->setHtml($html);
return $this;
});
2017-10-01 12:49:01 +08:00
// And then you can call by the name of the binding
2017-09-29 14:59:16 +08:00
$data = $ql->myHttp('https://toutiao.io')->find('h3 a')->texts();
print_r($data->all());
```
2017-10-01 12:49:01 +08:00
Or package to class, and then bind:
2017-09-29 14:59:16 +08:00
```php
$ql->bind('myHttp',function ($url){
return new MyHttp($this,$url);
});
```
2017-10-01 12:49:01 +08:00
#### Plugin used
- Use the PhantomJS plugin to crawl JavaScript dynamically rendered pages:
2017-09-30 21:46:31 +08:00
2017-09-30 21:49:07 +08:00
```php
2017-10-01 12:49:01 +08:00
// Set the PhantomJS binary file path during installation
2017-09-30 21:46:31 +08:00
$ql = QueryList::use(PhantomJs::class,'/usr/local/bin/phantomjs');
2017-10-01 12:49:01 +08:00
// Crawl「500px」all picture links
$data = $ql->browser('https://500px.com/editors')->find('img')->attrs('src');
2017-09-30 21:46:31 +08:00
print_r($data->all());
2017-10-01 12:49:01 +08:00
// Use the HTTP proxy
$ql->browser('https://500px.com/editors',false,[
2017-09-30 21:46:31 +08:00
'--proxy' => '192.168.1.42:8080',
'--proxy-type' => 'http'
])
```
2017-10-01 12:49:01 +08:00
- Using the CURL multithreading plug-in, multi-threaded crawling GitHub trending :
2017-09-30 21:46:31 +08:00
2017-09-29 15:05:33 +08:00
```php
2017-09-29 14:59:16 +08:00
$ql = QueryList::use(CurlMulti::class);
$ql->curlMulti([
'https://github.com/trending/php',
'https://github.com/trending/go',
//.....more urls
])
2017-10-01 12:49:01 +08:00
// Called if task is success
2017-09-29 14:59:16 +08:00
->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());
})
2017-10-01 12:49:01 +08:00
// Task fail callback
2017-09-29 14:59:16 +08:00
->error(function ($errorInfo,CurlMulti $curl){
echo "Current url:{$errorInfo['info']['url']} \r\n";
print_r($errorInfo['error']);
})
->start([
2017-10-01 12:49:01 +08:00
// Maximum number of threads
2017-09-29 14:59:16 +08:00
'maxThread' => 10,
2017-10-01 12:49:01 +08:00
// Number of error retries
2017-09-29 14:59:16 +08:00
'maxTry' => 3,
]);
```
2017-10-01 12:49:01 +08:00
## Plugins
- [jae-jae/QueryList-PhantomJS](https://github.com/jae-jae/QueryList-PhantomJS):Use PhantomJS to crawl Javascript dynamically rendered page.
- [jae-jae/QueryList-CurlMulti](https://github.com/jae-jae/QueryList-CurlMulti) : Curl multi threading.
- [jae-jae/QueryList-AbsoluteUrl](https://github.com/jae-jae/QueryList-AbsoluteUrl) : Converting relative urls to absolute.
2017-10-01 23:37:09 +08:00
- [jae-jae/QueryList-Rule-Google](https://github.com/jae-jae/QueryList-Rule-Google) : Google searcher.
- [jae-jae/QueryList-Rule-Baidu](https://github.com/jae-jae/QueryList-Rule-Baidu) : Baidu searcher.
2017-09-29 12:16:50 +08:00
2017-09-30 11:32:09 +08:00
2017-10-01 12:49:01 +08:00
View more QueryList plugins and QueryList-based products: [QueryList Community](https://github.com/jae-jae/QueryList-Community)
2017-09-30 01:12:00 +08:00
2017-10-01 12:49:01 +08:00
## Contributing
Welcome to contribute code for the QueryList。About Contributing Plugins can be viewed:[QueryList Plugin Contributing Guide](https://github.com/jae-jae/QueryList-Community/blob/master/CONTRIBUTING.md)
2017-09-29 00:36:02 +08:00
## Author
Jaeger <JaegerCode@gmail.com>
2017-10-01 12:49:01 +08:00
If this library is useful for you, say thanks [buying me a beer :beer:](https://www.paypal.me/jaepay)!
2017-09-29 00:36:02 +08:00
## Lisence
2017-09-22 22:37:25 +08:00
QueryList is licensed under the license of MIT. See the LICENSE for more details.