-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
120 lines (103 loc) · 3.7 KB
/
Copy pathexample.js
File metadata and controls
120 lines (103 loc) · 3.7 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Example script demonstrating how to use the web-extract-data package.
*/
const { WebExtractClient } = require('./index');
// Initialize the client with your InstantAPI.ai key
// Replace %% API_KEY %% with your API key from:
// https://web.instantapi.ai/#pricing-03-254921
const client = new WebExtractClient("%%API_KEY%%");
/**
* Example of using the scrape endpoint.
*/
async function exampleScrape() {
console.log("\n--- SCRAPE ENDPOINT EXAMPLE ---");
try {
// Extract monitor details from Amazon
const result = await client.scrape({
url: "https://www.amazon.com.au/MSI-PRO-MP341CQW-UltraWide-Compatible/dp/B09Y19TRQ2",
fields: {
"monitor_name": "< The product name of the monitor. >",
"brand": "< The brand or manufacturer name. >",
"display_size_in_inches": "< Numeric only. >",
"resolution": "< Example format: 1920x1080. >",
"panel_type": "< Type of panel. >",
"refresh_rate_hz": "< Numeric only. >",
"aspect_ratio": "< Example format: 16:9. >",
"ports": "< A comma-delimited list of available ports (e.g., HDMI, DisplayPort, etc.). >",
"features": "< Key selling points or capabilities, comma-delimited (e.g., LED, Full HD, etc.). >",
"price": "< Numeric price (integer or float). >",
"price_currency": "< Price currency (3 character alphabetic ISO 4217). >",
"review_count": "< Total number of customer reviews, numeric only. >",
"average_rating": "< Float or numeric star rating (e.g., 4.3). >",
"review_summary": "< A 50 words or less summary of all the written customer feedback. >"
}
});
console.log("Scrape Result:", JSON.stringify(result.scrape, null, 2));
} catch (error) {
console.error("Error:", error.message);
}
}
/**
* Example of using the links endpoint.
*/
async function exampleLinks() {
console.log("\n--- LINKS ENDPOINT EXAMPLE ---");
try {
// Extract product links from IKEA
const result = await client.links({
url: "https://www.ikea.com/au/en/cat/quilt-cover-sets-10680/?page=1",
description: "individual product urls"
});
console.log("Links Result:", JSON.stringify(result.links, null, 2));
} catch (error) {
console.error("Error:", error.message);
}
}
/**
* Example of using the next endpoint.
*/
async function exampleNext() {
console.log("\n--- NEXT ENDPOINT EXAMPLE ---");
try {
// Extract next page links from IKEA
const result = await client.next({
url: "https://www.ikea.com/au/en/cat/quilt-cover-sets-10680/"
});
console.log("Next Page Result:", JSON.stringify(result.next, null, 2));
} catch (error) {
console.error("Error:", error.message);
}
}
/**
* Example of using the search endpoint.
*/
async function exampleSearch() {
console.log("\n--- SEARCH ENDPOINT EXAMPLE ---");
try {
// Search for a product on Google
const result = await client.search({
query: "AVID POWER 20V MAX Lithium Ion Cordless Drill Set",
googleDomain: "www.google.com",
page: 1
});
console.log("Search Result:", JSON.stringify(result.search, null, 2));
} catch (error) {
console.error("Error:", error.message);
}
}
/**
* Run all examples.
*/
async function runExamples() {
console.log("Web Extract Data - Usage Examples");
console.log("=================================");
console.log("Replace %% API_KEY %% with your API key before running.");
console.log("Get your API key from: https://web.instantapi.ai/#pricing-03-254921");
// Comment out examples you don't want to run
await exampleScrape();
await exampleLinks();
await exampleNext();
await exampleSearch();
}
// Run the examples
runExamples().catch(console.error);