-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (60 loc) · 2.14 KB
/
index.js
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
const puppeteer = require("puppeteer");
const fs = require("fs/promises");
const coinMCScraper = async () => {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
args: ["--window-size=800,600"],
});
const page = await browser.newPage();
await page.goto("https://coinmarketcap.com/");
await page.evaluate(scrollToBottom);
await page.waitForTimeout(3000);
const coinName = await page.evaluate(() => {
return Array.from(
document.querySelectorAll(
"#__next > div > div.main-content > div.sc-57oli2-0.comDeo.cmc-body-wrapper > div > div > div.h7vnx2-1.bFzXgL > table > tbody > tr > td:nth-child(3) > div > a > div > div > p"
)
).map((x) => x.textContent);
});
const coinSign = await page.evaluate(() => {
return Array.from(
document.querySelectorAll(
"#__next > div > div.main-content > div.sc-57oli2-0.comDeo.cmc-body-wrapper > div > div > div.h7vnx2-1.bFzXgL > table > tbody > tr > td:nth-child(3) > div > a > div > div > div > p"
)
).map((x) => x.textContent);
});
const coinMarketCap = await page.evaluate(() => {
return Array.from(
document.querySelectorAll(
"#__next > div > div.main-content > div.sc-57oli2-0.comDeo.cmc-body-wrapper > div > div > div.h7vnx2-1.bFzXgL > table > tbody > tr > td:nth-child(7) > p > span.sc-1ow4cwt-1.ieFnWP"
)
).map((x) => x.textContent);
});
const top100Coins = [];
for (let i = 0; i < 100; i++) {
let coin = `Coin #${i + 1}: ${coinName[i]} * Sign: ${
coinSign[i]
} * MarketCap: ${coinMarketCap[i]}`;
top100Coins.push(coin);
}
await fs.writeFile("top100crypto.txt", top100Coins.join("\r\n\n"));
await browser.close();
};
const scrollToBottom = async () => {
await new Promise((resolve) => {
const distance = 100;
const delay = 100;
const timer = setInterval(() => {
document.scrollingElement.scrollBy(0, distance);
if (
document.scrollingElement.scrollTop + window.innerHeight >=
document.scrollingElement.scrollHeight
) {
clearInterval(timer);
resolve();
}
}, delay);
});
};
coinMCScraper();