• Stars
    star
    2,580
  • Rank 17,765 (Top 0.4 %)
  • Language
    PHP
  • Created almost 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🕷️ The progressive PHP crawler framework! 优雅的渐进式PHP采集框架。

QueryList

QueryList

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

API Documentation

中文文档

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
  • .....

Requirements

  • PHP >= 7.1

Installation

By Composer installation:

composer require jaeger/querylist

Usage

DOM Traversal and Manipulation

  • Crawl「GitHub」all picture links
QueryList::get('https://github.com')->find('img')->attrs('src');
  • 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
});
  • 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')->...

List crawl

Crawl the title and link of the Google search results list:

$data = QueryList::get('https://www.google.co.jp/search?q=QueryList')
	// Set the crawl rules
    ->rules([ 
	    'title'=>array('h3','text'),
	    'link'=>array('h3>a','href')
	])
	->query()->getData();

print_r($data->all());

Results:

Array
(
    [0] => Array
        (
            [title] => Angular - QueryList
            [link] => https://angular.io/api/core/QueryList
        )
    [1] => Array
        (
            [title] => QueryList | @angular/core - Angularリファレンス - Web Creative Park
            [link] => http://www.webcreativepark.net/angular/querylist/
        )
    [2] => Array
        (
            [title] => QueryListにQueryを追加したり、追加されたことを感知する | TIPS ...
            [link] => http://www.webcreativepark.net/angular/querylist_query_add_subscribe/
        )
        //...
)

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

HTTP Client (GuzzleHttp)

  • Carry cookie login GitHub
//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;....'
  ]
]);
//echo $ql->getHtml();
$userName = $ql->find('.header-nav-current-user>.css-truncate-target')->text();
echo $userName;
  • Use the Http proxy
$urlParams = ['param1' => 'testvalue','params2' => 'somevalue'];
$opts = [
	// Set the http proxy
    'proxy' => 'http://222.141.11.17:8118',
    //Set the timeout time in seconds
    'timeout' => 30,
     // Fake HTTP headers
    '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();
  • Analog login
// Post login
$ql = QueryList::post('http://xxxx.com/login',[
    'username' => 'admin',
    'password' => '123456'
])->get('http://xxx.com/admin');
// Crawl pages that need to be logged in to access
$ql->get('http://xxx.com/admin/page');
//echo $ql->getHtml();

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 !';
}

Bind function extension

Customize the extension of a myHttp method:

$ql = QueryList::getInstance();

//Bind a `myHttp` method to the QueryList object
$ql->bind('myHttp',function ($url){
	// $this is the current QueryList object
    $html = file_get_contents($url);
    $this->setHtml($html);
    return $this;
});

// And then you can call by the name of the binding
$data = $ql->myHttp('https://toutiao.io')->find('h3 a')->texts();
print_r($data->all());

Or package to class, and then bind:

$ql->bind('myHttp',function ($url){
    return new MyHttp($this,$url);
});

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'
])
  • 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,
]);

Plugins

View more QueryList plugins and QueryList-based products: QueryList Community

Contributing

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

Author

Jaeger [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.

More Repositories

1

Userscript-Plus

🐒 Show current site all UserJS,The easier way to install UserJs for Tampermonkey. 显示当前网站的所有可用Tampermonkey脚本
Vue
1,651
star
2

Camtd

Chrome multi-threaded download manager extension,based on Aria2 and AriaNg. Chrome多线程下载扩展。
JavaScript
924
star
3

weapp-github-trending

微信小程序:Github今日榜单
JavaScript
278
star
4

docker-pan

Docker:Filerun+AriaNg+Aria2,Personal cloud disk 搭建个人的可离线云盘
Shell
183
star
5

phpQuery-single

phpQuery onefile composer,phpQuery单文件版本,是Querylist的依赖(http://querylist.cc )
PHP
131
star
6

QueryList-Community

There are QueryList plugins and QueryList-based projects from open-source contributors. 来自社区的QueryList插件和基于QueryList 的作品.
76
star
7

chrome-extension-gulu

Chrome 扩展轱辘(脚手架),使用 Vue.js + webpack 来开发和打包Chrome扩展, 支持热重载。
JavaScript
24
star
8

CurlMulti

CurlMulti是PHP多线程类库,修改用于QueryList(http://querylist.cc ),CurlMulti项目地址:https://github.com/ares333/php-curlmulti
PHP
20
star
9

QueryList-CurlMulti

QueryList Plugin: Curl multi threading. QueryList Curl多线程插件
PHP
20
star
10

QueryList-Puppeteer

QueryList Plugin: Use Puppeteer to crawl Javascript dynamically rendered pages.(Headless Chrome ) 使用Puppeteer采集JavaScript动态渲染的页面
PHP
19
star
11

GHttp

基于GuzzleHttp的简单版Http客户端。 Simple Http client base on GuzzleHttp
PHP
17
star
12

QueryList-Rule-Baidu

QueryList Plugin: Baidu searcher. QueryList百度搜索插件,百度搜索采集,百度搜索爬虫。
PHP
15
star
13

docker-v2fly-privoxy

Docker版 v2ray 客户端,v2ray 结合 privoxy 来使用 gfwlist 规则来实现代理过滤, 并实现 socks5 代理转 http 代理,可用于群晖。
Dockerfile
14
star
14

QueryList-Rule-Google

QueryList Plugin: Google searcher,Google Search Engine Scraper in PHP. QueryList谷歌搜索插件
PHP
13
star
15

Http

HTTP 请求类, 支持 CURL 和 Socket, 默认使用 CURL , 当手动指定seCurl 或者 curl 扩展没有安装时, 会使用 Socket目前支持 get 和 post 两种请求方式 ,修改用于QueryList(http://querylist.cc )
PHP
9
star
16

uTools-Finder

uTools 在 MacOS 下的文件搜索插件
Vue
9
star
17

QueryList-AbsoluteUrl

QueryList Plugin: Converting relative urls to absolute. 转换URL相对路径到绝对路径
PHP
8
star
18

docker-baota

Docker:宝塔Linux面板,https://www.bt.cn
Shell
8
star
19

amh4.2-xg

amh4.2西瓜哥基于vpskk修改版
Shell
7
star
20

FileUtil

PHP Library for simple to create/move/delete file/dir (PHP文件操作)
PHP
7
star
21

QueryList-Ext-AQuery

QueryList扩展,AQuery扩展基类
PHP
7
star
22

QueryList-Ext-Login

QueryList扩展,Login模拟登陆扩展
PHP
5
star
23

e-search

Elasticsearch5 PHP Api
PHP
5
star
24

solusvm-client

SolusVM PHP Api,PHP control vps reboot, shutdown, boot, status。-通过PHP来管理和控制VPS
PHP
5
star
25

QueryList-Ext-DImage

QueryList扩展,DImage图片下载扩展
PHP
4
star
26

uTools-Zhtw

uTools 插件:繁体字与简体字互转
Vue
4
star
27

QueryList-Ext-Request

QueryList扩展,Request网络操作扩展
PHP
3
star
28

QueryList-Ext-Multi

QueryList扩展,Multi多线程扩展
PHP
2
star
29

Ubuntu-getFastMirror

Ubuntu获取最快的镜像脚本
Shell
2
star
30

docker-shadowsocksR

docker:shadowsocksR,https://shadowsocks.be/9.html
Shell
2
star
31

gush

简易的全平台项目部署工具
Go
2
star
32

Alfred-Workflows

Alfred Workflows
1
star
33

gulp-insert-string-into-tag

gulp插入字符串到指定标签之间的插件
JavaScript
1
star
34

collection

Golang collection package implements simple functions to manipulate arrays , slices and map,Imitate Laravel Collections.
Go
1
star