-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.php
More file actions
65 lines (56 loc) · 2.3 KB
/
Copy pathscraper.php
File metadata and controls
65 lines (56 loc) · 2.3 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
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
/**
* Runnable example: scraper endpoints.
*
* Set NOVADA_API_KEY in the environment, then run:
* php examples/scraper.php
*/
require __DIR__ . '/../vendor/autoload.php';
use Novada\Client;
use Novada\Exception\ApiException;
use Novada\Exception\AuthException;
use Novada\Exception\RateLimitException;
use Novada\Exception\ValidationException;
use Novada\Scraper\Dto\Request as ScrapeRequest;
use Novada\Scraper\Dto\UnblockerParams;
use Novada\Scraper\Dto\YouTubeVideoParams;
use Novada\Scraper\Target;
$client = new Client(); // reads NOVADA_API_KEY from the environment
try {
// Strongly typed YouTube scraper (auto-selects the Scraper API host).
$video = $client->scraper->api->youtube->videoPost(new YouTubeVideoParams(
url: 'https://www.youtube.com/watch?v=HAwTwmzgNc4',
));
printf("youtube video-post raw length=%d\n", strlen($video->raw));
// Generic driver — any scraper_id, host chosen explicitly.
$generic = $client->scraper->do(new ScrapeRequest(
target: Target::ScraperApi,
scraperName: 'youtube.com',
scraperId: 'youtube_video-post_explore',
params: [['url' => 'https://www.youtube.com/watch?v=HAwTwmzgNc4']],
returnErrors: true,
));
printf("generic do() raw length=%d\n", strlen($generic->raw));
// Web Unblocker — typed scrape, structured result.
$unblocked = $client->scraper->unblocker->scrape(new UnblockerParams(
targetUrl: 'https://www.google.com',
country: 'us',
));
printf("unblocker code=%d html_length=%d use_balance=%f\n", $unblocked->code, strlen($unblocked->html), $unblocked->useBalance);
// Query endpoints on the general host.
$balance = $client->scraper->universal->balance();
printf("scraper balance=%f\n", $balance->scraperBalance);
} catch (ValidationException $e) {
fwrite(STDERR, "validation error in {$e->getMethod()}: " . implode(', ', $e->getFields()) . "\n");
exit(1);
} catch (AuthException $e) {
fwrite(STDERR, "invalid API key (HTTP {$e->getHttpStatus()})\n");
exit(1);
} catch (RateLimitException $e) {
fwrite(STDERR, "rate limited (HTTP {$e->getHttpStatus()})\n");
exit(1);
} catch (ApiException $e) {
fwrite(STDERR, "api error code={$e->getCode()} http={$e->getHttpStatus()}: {$e->getMsg()}\n");
exit(1);
}