cacing69 / cquery
PHP Scraper with language expression, could be used to scrape data from a website that uses javascript or ajax
Fund package maintenance!
cacing69
Ko Fi
Liberapay
saweria.co/cacing69
trakteer.id/cacing69/tip
Requires
- php: ^7.2|^8.1
- cocur/slugify: ^4.4
- doctrine/collections: ^1.8|^2.1|^3.0
- symfony/browser-kit: ^5.4|^6.3
- symfony/css-selector: ^5.4|^6.3
- symfony/deprecation-contracts: ^2.5|^3.4
- symfony/dom-crawler: 5.4|^6.3
- symfony/http-client: ^5.4|^6.3
- symfony/mime: 5.4|^6.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- phpunit/phpunit: ^8.0|^9.0|^10.0
- symfony/var-dumper: ^5.4|^6.4
README
Cquery (Crawl Query)
About Cquery
Want to create a query for web scraping a website like this, I suppose it will enable me to generate scrape queries from anywhere.
from (.item) define span > a.title as title attr(href, div > h1 > span > a) as url filter span > a.title has 'history' limit 1
Changelog
Please see CHANGELOG for more information what has changed recently.
Currently experimenting
Attempt to extract data from webpage which, in my opinion, becomes more enjoyable, my intention in creating this was to enable web scraping of websites that utilize js/ajax for content loading.
To perform web scraping on pages loaded with js/ajax, you need an adapter outside of this package, this was developed using symfony/panther
. I don't want to add it as a default package in the core of cquery because, this feature is optional for some people. Please check and understand its usage here. I refer to it as cacing69/cquery-panther-loader. Read more information about symfony/panther, you'll discover installation and additional information there
All methods and usage instructions provided here are designed according to that i needs. If you have any suggestions or feedback to improve them, it would be highly appreciated and
I hope there's someone who is kind-hearted and compassionate to build a Web App/UI application like a dedicated tool for cquery, with a textarea (for query input) and a table container to show the results, much like the cquery playground for running raw cquery, if someone is ready, i will create an API for it, and start to develop more logic on Parser class
What kind of thing is this
Cquery is an acronym for crawl query, used to extract text from an HTML element using PHP, simply its tool for crawling/scraping web page. It called a query, as it adopts the structure present in an SQL query, so you can analogize that your DOM/HTML Document is a table you will query.
Let's play for a moment and figure out how to make website scraping easier, much like crafting a query for a database.
Please keep in mind that I haven't yet reached a beta/stable release for this library, so the available features are still very limited.
I would greatly accept any support/contribution from everyone. See CONTRIBUTING.md for help getting started.
I list a few examples of utilizing the advanced features
- Manipulate Query Result
- Multiple Requests (to get detail from another url)
- Doing action after page load (click link/submit form)
- Scrape website load by js/ajax with PHP
Quick Installation
composer require cacing69/cquery
For example, you have a simple HTML element as shown below.
Click to show HTML : src/Samples/sample.html
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Href Attribute Example</title> </head> <body> <span id="lorem"> <div class="link valid"> <h1 data-link-from-source="/url-1" id="title-id-1" class="class-title-1">Title 1</h1> <a class="ini vip class-1" data-custom-attr-id="12" href="http://ini-url-1.com">Href Attribute Example 1 </a> </div> <div class="link"> <h1 ref-id="23" id="title-id-2" class="class-title-1">Title 2</h1> <a class="vip class-2 nih tenied" data-custom-attr-id="212" href="http://ini-url-2.com"> Href Attribute Example 2 <p>Lorem pilsum</p> </a> </div> <div class="link"> <h1 id="title-id-3" class="class-title-1">Title 3</h1> <a class="premium class-3" data-custom-attr-id="122" regex-test="a-abc-ab" href="http://ini-url-3.com">Href Attribute Example 4</a> </div> <div class="link"> <h1>Title 11</h1> <a class="vip class-1 super blocked" data-custom-attr-id="132" regex-test="a-192-ab" href="http://ini-url-11.com">Href Attribute Example 78</a> </div> <div class="link"> <h1>Title 22</h1> <a class="preview itu class-2 vip blocked" data-custom-attr-id="712" regex-test="b-12-ac" href="http://ini-url-22.com">Href Attribute Example 90</a> </div> <div class="link"> <h1>Title 323</h1> <a class="nied premium class-3 blocked" data-custom-attr-id="132" href="http://ini-url-33-1.com">Href Attribute Example 5</a> </div> <div class="link pending"> <h1>Title 331</h1> <a class="premium class-31 ended" data-custom-attr-id="121" regex-test="zx-1223-ac" customer-id="18" href="http://ini-url-33-2.com">Href Attribute Example 51</a> </div> <div class="link pending"> <h1>Title 331</h1> <a class="test-1-item" data-custom-attr-id="121" customer-id="16" href="http://ini-url-33-2.com">Href Attribute Example 51</a> </div> <div class="link pending"> <h1>12345</h1> <a class="premium class-32 denied" data-custom-attr-id="1652" customer-id="17" href="http://ini-url-33-0.com">Href Attribute Example 52</a> </div> </span> <p> <a href="https://www.freecodecamp.org/contribute/">The freeCodeCamp Contribution Page </p> <footer> <p>Copyright 2023</p> </footer> </body> </html>
List definer expression available
Below are the expressions you are can use, they may change over time.
List rules for alias
Below are the functions you are can use, they may change over time.
Note: nested function has been supported.
How to use filter
Note: nested filter not supported yet.
So, let's start scraping this website.
require_once 'vendor/autoload.php'; $html = file_get_contents("src/Samples/sample.html"); $data = new Cacing69\Cquery\Cquery($html); $result = $query ->from("#lorem .link") // next will be from("(#lorem .link) as el") ->define( "h1 as title", "a as description", "attr(href, a) as url", // get href attribute from all element at #lorem .link a "attr(class, a) as class" ) // just imagine this is your table, and every element as your column ->filter("attr(class, a)", "has", "vip") // add some filter here // ->orFilter("attr(class, a)", "has", "super") // add another condition its has OR condition SQL // ->filter("attr(class, a)", "has", "blocked") // add another condition its has AND condition SQL ->get(); // -> return type is \Doctrine\Common\Collections\ArrayCollection
or u can use raw method
require_once 'vendor/autoload.php'; $html = file_get_contents("src/Samples/sample.html"); $data = new Cacing69\Cquery\Cquery($html); $result = $query ->raw(" from (#lorem .link) define h1 as title, a as description, attr(href, a) as url, attr(class, a) as class filter attr(class, a) has 'vip' ");
And here are the results
Another example with anonymous function
require_once 'vendor/autoload.php'; use Cacing69\Cquery\Definer; $html = file_get_contents("src/Samples/sample.html"); $data = new Cacing69\Cquery\Cquery($html); $result_1 = $data ->from("#lorem .link") ->define( "upper(h1) as title_upper", new Definer( "a", "col_2", function($value) use ($date) { return "{$value} fetched on: {$date}"; }) ) ->filter("attr(class, a)", "has", "vip") ->limit(2) ->get() // -> return type is \Doctrine\Common\Collections\ArrayCollection ->toArray();
// another example, filter with closure $result_2 = $data ->from("#lorem .link") ->define("reverse(h1) as title", "attr(href, a) as url") ->filter("h1", function ($e) { return $e->text() === "Title 3"; }) ->get() // -> return type is \Doctrine\Common\Collections\ArrayCollection ->toArray();
How to load source page from url
// another example, to load data from url used browserkit $url = "https://free-proxy-list.net/"; $data = new Cquery($url); $result_3 = $data ->from(".fpl-list") ->pick( "td:nth-child(1) as ip_address", "td:nth-child(4) as country", "td:nth-child(7) as https", )->filter('td:nth-child(7)', "=", "no") ->limit(1) ->get() // -> return type is \Doctrine\Common\Collections\ArrayCollection ->toArray();
how to use append_node(a, b)
// another example, to load data from url used browserkit $url = "http://quotes.toscrape.com/"; $data = new Cquery($url); $result_4 = $data ->from(".col-md-8 > .quote") ->define( "span.text as text", "span:nth-child(2) > small as author", "append_node(div > .tags, a) as tags", ) ->get() // -> return type is \Doctrine\Common\Collections\ArrayCollection ->toArray();
Another example how to use append_child() with custom key each item
// another example, to load data from url used browserkit $url = "http://quotes.toscrape.com/"; $data = new Cquery($url); $result_5 = $data ->from(".col-md-8 > .quote") ->define( "span.text as text", "append_node(div > .tags, a) as tags.key", // grab child `a` on element `div > .tags` and place it into tags['key'] ) ->get() // -> return type is \Doctrine\Common\Collections\ArrayCollection ->toArray();
// another example, to load data from url used browserkit $url = "http://quotes.toscrape.com/"; $data = new Cquery($url); $result_6 = $data ->from(".col-md-8 > .quote") ->define( "span.text as text", "append_node(div > .tags, a) as _tags", "append_node(div > .tags, a) as tags.*.text", "append_node(div > .tags, attr(href, a)) as tags.*.url", // [*] means each index, for now ots limitd only one level ) ->get() // -> return type is \Doctrine\Common\Collections\ArrayCollection ->toArray();
How to use replace
// how to use replace with single string $content = file_get_contents(SAMPLE_HTML); $data = new Cquery($content); $result = $data ->from(".col-md-8 > .quote") ->define( "replace('The', 'Lorem', span.text) as text", ) ->get(); // how to use replace with array arguments $data_2 = new Cquery($content); $result = $data_2 ->from(".col-md-8 > .quote") ->define( "replace(['The', 'are'], ['Please ', 'son'], span.text) as text", // "replace(['The', 'are'], ['Please'], span.text) as text", // or you can do this if just want to use single replacement ) ->get(); // how to use replace with array arguments and single replacement $data_3 = new Cquery($simpleHtml); $result = $data_3 ->from("#lorem .link") ->define("replace(['Title', '331'], 'LOREM', h1) as title") ->get();
Method to manipulate query results
There are 2 methods in CQuery for manipulating query results.- Each Item Closure
...->eachItem(function ($el, $i){})
or...->eachItem(function ($el){})
Example :
...->eachItem(function ($item, $i){ $item["price"] = $i == 2 ? 1000 : $resultDetail["price"]; return $item; })
Basically, you have the ability to execute any action on each item. In the given example, it will insert a new key, "price" into each item, and if the index equals 2 (third item), it will assign a price of 1000.
- On Obtained Results Closure
...->onObtainedResults(function ($results){})
Example :
...->onObtainedResults(function ($results){ // u can do any operation here return array_map(function ($_item) use ($results) { $_item["sub"] = [ "foo" => "bar" ]; return $_item; }, $results); })
Basically, this is the array produced by the query's result, and you have the flexibility to perform any manipulations on them. For another example i've included an example, particularly for cases where you need to load different details from another page for each entry, u can check it here Check async multiple request
How to handle multiple request each element
If there's a scenario like this, you need to load the details, and the details are on a different URL, which means you have to load every page.You should use a client that can perform non-blocking requests, such as amphp/http-client, guzzle, phpreact/http or used curl_multi_init in oop ways for curl u should check php-curl-class
I suggest using phpreact by making async requests.
use Cacing69\Cquery\Cquery; use React\EventLoop\Loop; use React\Http\Browser; use Psr\Http\Message\ResponseInterface; $url = "http://www.classiccardatabase.com/postwar-models/Cadillac.php"; $data = new Cquery($url); $loop = Loop::get(); $client = new Browser($loop); // detail is on another page $result = $data ->from(".content") ->define( ".car-model-link > a as name", "replace('../', 'http://www.classiccardatabase.com/', attr(href, .car-model-link > a)) as url", ) ->filter("attr(href, .car-model-link > a)", "!=", "#") ->onObtainedResults(function ($results) use ($loop, $client){ // I've come across a maximum threshold of 25 chunk, when I input 30, there is some null data. $results = array_chunk($results, 25); foreach ($results as $key => $_chunks) { foreach ($_chunks as $_key => $_result) { $client ->get($_result["url"]) ->then(function (ResponseInterface $response) use (&$results, $key, $_key) { $detail = new Cquery((string) $response->getBody()); $resultDetail = $detail ->from(".spec") ->define( ".specleft tr:nth-child(1) > td.data as price" ) ->first(); $results[$key][$_key]["price"] = $resultDetail["price"]; }); } $loop->run(); } return $results; }) ->get();
Here's a comparison when utilizing phpreact.
without phpreact
with phpreact
In this scenario, there are 320 rows of data, and each detail will be loaded, which means there will a lot of HTTP requests made to fetch the individual details.
How to doing action after page load (click link/submit form)
1. Submit FormIf you need to submit data to retrieve another data for scraping, you'll need to deal with this case.
- case 1 : without crawler object
$url = "https://user-agents.net/random"; $data = new Cquery($url); $result = $data ->onContentLoaded(function (HttpBrowser $browser) { $browser->submitForm("Generate random list", [ "limit" => 5, ]); return $browser; }) ->from("section > article") ->define( "ol > li > a as user_agent", ) ->get();
Using this code above, you'll perform a form submission while setting the limit (according to input name) to 5 in the data.
- case 2 : with crawler object
Let's simulate on Wikipedia and then perform a search with the phrase 'sambas,' to see if the results match with a manual search.
$url = "https://id.wikipedia.org/wiki/Halaman_Utama"; $data = new Cquery($url); $result = $data ->onContentLoaded(function (HttpBrowser $browser, Crawler $crawler) { // This is a native function available in the dom-crawler. $form = new Form($crawler->filter("#searchform")->getNode(0), $url); $browser->submit($form, [ "search" => "sambas", ]); return $browser; }) ->from("html") ->define( "title as title", ) ->get();
result
web page
page source
- Click Link If you want to click a link on a loaded page, please observe the code below.
click that link before start scraping
$url = "https://semver.org/"; $data = new Cquery($url); $result = $data ->onContentLoaded(function (HttpBrowser $browser, Crawler $crawler) { $browser->clickLink("Bahasa Indonesia (id)"); return $browser; }) ->from("#spec") ->define( "h2 as text", ) ->get();
result click link
How to scrape website load by js/ajax with PHP
If the web page to be scraped uses JavaScript and AJAX handling for its data, then you need to add Panther-loader for cquery.
install composer-panther-loader
composer require cacing69/cquery-panther-loader
Another Examples
A full list of methods with example code can be found in the tests.
Note
I've recently started building this, and if anyone is interested,I would certainly appreciate a lot of feedback from everyone who has read/seen my little project, in any way (issue, pull request or whatever).However, right now I'm considering making it better to be more flexible and user-friendly for website scraping.
This is just the beginning, and I will continue to develop it as long as I can
License
The MIT License (MIT). Please see License File for more information.