一个极其快速的 Web 抓取器,可在一瞬间解析数百 MB 的无效 HTML。PHP 5.3 以上,无依赖。
An extremely fast and efficient web scraper that can parse megabytes of invalid HTML in a blink of an eye.
You can use the familiar jQuery/CSS selector syntax to easily find the data you need.
In my unit tests, I demand it be at least 10 times faster than Symfony's DOMCrawler on a 3Mb HTML document. In reality, according to my humble tests, it is two-three orders of magnitude faster than DOMCrawler in some cases, especially when selecting thousands of elements, and on average uses x2 less RAM.
See tests/README.md.
mbstring extension is recommended for reliable charset handling and conversionsmemory_limit when working with very large documentsAdd the library to your project and include it, or install via Composer/npm.
Using Composer (recommended):
composer require duzun/hqueryOr include manually:
include_once '/path/to/hquery.php/hquery.php';Or via npm:
npm install hquery.phpThen require the file from node_modules if needed.
// Optionally use namespaces
use duzun\hQuery;
// Either use composer, or include this file:
include_once '/path/to/libs/hquery.php';
// Set the cache path - must be a writable folder
// If not set, hQuery::fromURL() would make a new request on each call
hQuery::$cache_path = "/path/to/cache";
// Time to keep request data in cache, seconds
// A value of 0 disables cache
hQuery::$cache_expires = 3600; // default one hourI would recommend using php-http/cache-plugin with a PSR-7 client for better flexibility.
$filename, boolean $use_include_path = false, resource $context = NULL )// Local
$doc = hQuery::fromFile('/path/to/filesystem/doc.html');
// Remote
$doc = hQuery::fromFile('https://example.com/', false, $context);Where $context is created with stream_context_create().
For an example of using $context to make a HTTP request with proxy see #26.
$html, string $url = NULL )$doc = hQuery::fromHTML('Sample HTML DocContents...');
// Set base_url, in case the document is loaded from local source.
// Note: The base_url property is used to retrieve absolute URLs from relative ones.
$doc->base_url = 'http://desired-host.net/path';$url, array $headers = NULL, array|string $body = NULL, array $options = NULL )…For building advanced requests (POST, parameters etc) see hQuery::http_wr(),
though I recommend using a specialized (PSR-7?) library for making requests
and hQuery::fromHTML($html, $url=NULL) for processing results.
See Guzzle for eg.
composer require php-http/message php-http/discovery php-http/curl-clientIf you don't have cURL PHP extension,
just replace php-http/curl-client with php-http/socket-client in the above command.
use duzun\hQuery;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
$client = HttpClientDiscovery::find();
$messageFactory = MessageFactoryDiscovery::find();
$request = $messageFactory->createRequest(
'GET',
'http://example.com/someDoc.html',
['Accept' => 'text/html,application/xhtml+xml;q=0.9,*/*;q=0.8']
);
$response = $client->sendRequest($request);
$doc = hQuery::fromHTML($response, $request->getUri());
Another option is to use stream_context_create()
to create a $context, then call hQuery::fromFile($url, false, $context).
$sel, array|string $attr = NULL, hQuery\Node $ctx = NULL )…Charset and positions:
Note: In case the charset meta attribute has a wrong value or the internal conversion fails for any other reason, hQuery will continue processing with the original HTML, but will register an error message on $doc->html_errors['convert_encoding'].
On DUzun.Me
A lot of people ask for sources of my Live Demo page. Here we go:
view-source:https://duzun.me/playground/hquery
You can easily run any of the examples/ on your local machine.
All you need is PHP installed in your system.
After you clone the repo with git clone https://github.com/duzun/hQuery.php.git,
you have several options to start a web-server.
cd hQuery.php/examples
php -S localhost:8000
# open browser http://localhost:8000/This option starts a live-reload server and is good for playing with the code.
npm install
gulp
# open browser http://localhost:8080/If you are using VSCode, simply open the project and run debugger (F5).
I love Open Source. Whenever possible I share cool things with the world (check out NPM and GitHub).
If you like what I'm doing and this project helps you reduce time to develop, please consider to:
bitcoin:3MVaNQocuyRUzUNsTbmzQC8rPUQMC9qafa (or using the QR below)暂无开放 Issues,或尚未同步最近议题。