-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
55 lines (47 loc) · 1.52 KB
/
Copy pathexample.php
File metadata and controls
55 lines (47 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* Quasar — runnable demo. `php example.php`
*
* @author Vxsilisk — Quasar
* @license MIT
*/
require __DIR__ . '/autoload.php';
$html = <<<'HTML'
<!doctype html>
<html>
<head><title>Nebula — Latest Posts</title></head>
<body>
<main>
<article class="post" data-id="1">
<h2><a href="https://nebula.dev/pulsar">PulsarX 1.1 released</a></h2>
<p class="lead">Retries, backoff, per-request timeouts.</p>
<ul class="tags"><li>http</li><li>php</li><li class="hot">scraping</li></ul>
</article>
<article class="post draft" data-id="2">
<h2><a href="https://nebula.dev/quasar">Meet Quasar</a></h2>
<p class="lead">A zero-dep HTML scraper.</p>
</article>
</main>
</body>
</html>
HTML;
$doc = Quasar::html($html);
echo "Title: ", $doc->title(), "\n\n";
echo "Published posts (skipping drafts):\n";
$doc->find('article:not(.draft)')->each(function ($post) {
$link = $post->first('h2 a');
printf(" • %-24s %s\n", $link->text(), $link->attr('href'));
echo " tags: ", implode(', ', $post->find('.tags li')->texts()), "\n";
});
echo "\nAll links on the page:\n";
foreach ($doc->links() as $href) {
echo " - {$href}\n";
}
echo "\nHottest tag: ", $doc->first('li.hot')->text(), "\n";
// Fetch-and-parse, the intended pairing with PulsarX:
//
// $r = (new Pulsar())->impersonate('chrome')->get('https://news.ycombinator.com');
// $doc = Quasar::html($r->getBody());
// foreach ($doc->find('.titleline > a') as $story) {
// echo $story->text(), "\n";
// }