-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.cpp
More file actions
51 lines (45 loc) · 1.9 KB
/
Copy pathscraper.cpp
File metadata and controls
51 lines (45 loc) · 1.9 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
// scraper.cpp — run a Google search scrape and a Web Unblocker scrape.
//
// Reads the API key from the NOVADA_API_KEY environment variable. Build with
// -DNOVADA_BUILD_EXAMPLES=ON.
#include <iostream>
#include <novada/client.hpp>
#include <novada/errors.hpp>
#include <novada/scraper/api_service.hpp>
#include <novada/scraper/scraper_service.hpp>
#include <novada/scraper/unblocker_service.hpp>
int main() {
try {
novada::Client client;
// Structured Google Search scrape.
novada::scraper::GoogleSearchParams google;
google.query = "novada proxy";
google.country = "us";
auto result = client.scraper().api().google().search(google);
std::cout << "google code=" << result.code << " cost_time=" << result.cost_time << "ms\n";
std::cout << " task_id=" << result.data.task_id << "\n";
// Web Unblocker scrape — returns the rendered HTML.
novada::scraper::UnblockerParams unblock;
unblock.target_url = "https://example.com";
unblock.js_render = true;
auto page = client.scraper().unblocker().scrape(unblock);
std::cout << "unblocker code=" << page.code << " html_bytes=" << page.html.size()
<< " use_balance=" << page.use_balance << "\n";
// Generic scrape via do_request (any scraper_id).
novada::scraper::ScrapeRequest req;
req.target = novada::scraper::Target::kScraperAPI;
req.scraper_name = "youtube.com";
req.scraper_id = "youtube_video-post_explore";
req.params = {{{"url", "https://youtu.be/dQw4w9WgXcQ"}}};
auto raw = client.scraper().do_request(req);
std::cout << "youtube raw_bytes=" << raw.raw.size() << "\n";
return 0;
} catch (const novada::ApiException& e) {
std::cerr << "API error (http=" << e.http_status() << ", code=" << e.code()
<< "): " << e.message() << "\n";
return 1;
} catch (const novada::NovadaException& e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
}