Skip to content

Instantly share code, notes, and snippets.

@kami4ka
Created November 23, 2021 08:41
Show Gist options
  • Save kami4ka/dbc589e8e2e0edcb593c4b8fe0851478 to your computer and use it in GitHub Desktop.
Save kami4ka/dbc589e8e2e0edcb593c4b8fe0851478 to your computer and use it in GitHub Desktop.
Scrape new tokens from CoinMarketCap using ScrapingAnt API
/**
* Get data from new CoinMarketCap token listings
*
* ScrapingAnt allows you to scrape for free using proxy servers
*
* npm install @scrapingant/scrapingant-client
* npm install cheerio
**/
const cheerio = require('cheerio');
const ScrapingAnt = require('@scrapingant/scrapingant-client');
const API_KEY = '<SCRAPINGANT_API_KEY>';
const TOKENS_URL = 'https://coinmarketcap.com/new/';
const BASE_URL = 'https://coinmarketcap.com';
const client = new ScrapingAnt({ apiKey: API_KEY });
main()
.then(console.log)
.catch(console.error);
async function main() {
// Get all profile URLs
const tokens = [];
const tokensResponse = await client.scrape(TOKENS_URL, { browser: false });
const $ = cheerio.load(tokensResponse.content)
const rows = $('.cmc-table > tbody > tr');
rows.each((i, el) => {
const columns = $(el).find('td').toArray();
const oneHourSign = $(columns[4]).find('.icon-Caret-up').length > 0 ? '+' : '-';
const oneDaySign = $(columns[5]).find('.icon-Caret-up').length > 0 ? '+' : '-';
tokens.push({
url: BASE_URL + $(columns[2]).find('a').attr('href'),
name: $(columns[2]).find('p').first().text(),
symbol: $(columns[2]).find('p').last().text(),
price: $(columns[3]).text(),
oneHour: oneHourSign + $(columns[4]).text(),
oneDay: oneDaySign + $(columns[5]).text(),
marketCap: $(columns[6]).text(),
volume: $(columns[7]).text(),
blockchain: $(columns[8]).text(),
});
});
return tokens;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment