-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
115 lines (100 loc) · 2.82 KB
/
Copy pathindex.js
File metadata and controls
115 lines (100 loc) · 2.82 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
/* eslint-disable @typescript-eslint/no-var-requires, no-console */
const { ZenRows } = require("zenrows");
const apiKey = "YOUR-API-KEY";
const urlLinks = "https://www.scrapingcourse.com/";
const urlPremium = "https://www.google.com/search?q=Ariana+Grande";
const testPost = "https://httpbin.org/anything";
(async () => {
const client = new ZenRows(apiKey, { concurrency: 5, retries: 1 });
try {
const response = await client.get(urlLinks, {
css_extractor: '{"links": "a @href"}',
js_render: true,
premium_proxy: true,
});
const data = await response.json();
console.log(data.links);
/*
[
'https://www.zenrows.com',
'https://www.zenrows.com/blog',
...
]
*/
} catch (error) {
console.error(error.message);
if (error.response) {
console.error(error.response.data);
}
}
try {
const response = await client.get(urlPremium, {
// autoparse: true,
css_extractor: '{"aproximate_results": "#result-stats", "headers": "#search a > h3"}',
// js_render: true,
premium_proxy: true,
proxy_country: "us",
});
const data = await response.json();
console.log(data);
/*
{
aproximate_results: 'About 10,700,000 results (0.48 seconds)',
headers: [
'Ariana Grande | Home',
'Ariana Grande | Facebook',
'Best Ariana Grande Songs: 20 Essential Tracks - uDiscover ...',
...
]
}
*/
} catch (error) {
console.error(error.message);
if (error.response) {
console.error(error.response.data);
}
}
try {
const urls = [
"https://api.ipify.org",
"https://httpbin.org/ip",
"https://httpbin.org/anything",
"https://ident.me",
];
const promises = urls.map((url) => client.get(url));
const results = await Promise.allSettled(promises);
const rejected = results.filter(({ status }) => status === "rejected");
const fulfilled = results.filter(({ status }) => status === "fulfilled");
console.log(rejected);
console.log(fulfilled);
} catch (error) {
console.error(error.message);
if (error.response) {
console.error(error.response.data);
}
}
try {
const response = await client.post(
testPost,
{},
{
data: new URLSearchParams({
key1: "value1",
key2: "value2",
}).toString(),
},
);
const data = await response.json();
console.log(data);
/*
...
form: { key1: 'value1', key2: 'value2' },
...
*/
} catch (error) {
console.error(error.message);
if (error.response) {
console.error(error.response.data);
}
}
})();