0

I am always getting an Error while trying to scrape something from coinfarm.online. So I want the last price. When I am trying it with the console inside the browser it works perfect, but with this script I am always getting an error or null.

const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch({ headless: true });
  const page = await browser.newPage();
  await page.goto("https://coinfarm.online", { waitUntil: "load", timeout: 0 });

  const example = await page.evaluate(
    () => document.querySelector("#xbt_last").innerText
  );

  console.log("Price: " + example);

  await browser.close();
})();

I also tried it with the XPath but also didn't work....

8Bit
  • 1
  • 1

1 Answers1

0

I've made this for you

const puppeteer = require ('puppeteer')
const uaString = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3882.0 Safari/537.36'

;(async () => {
    const browser = await puppeteer.launch ({
        headless : true,
        devtools : false
    })
    const [page] = await browser.pages()

    page.setDefaultNavigationTimeout(0)
    page.setUserAgent(uaString)
    page.setRequestInterception(true)

    page.on('request', async request => {
        if ( request.resourceType() === 'image' || request.resourceType() === 'font' || request.resourceType() === 'media' ) {
            request.abort ()
        } else {
            request.continue ()
        }
    })

    const open = await page.goto ('https://s.tradingview.com/embed-widget/tickers/?locale=en#%7B%22symbols%22%3A%5B%7B%22description%22%3A%22BitMex%20XBT%22%2C%22proName%22%3A%22BITMEX%3AXBTUSD%22%7D%2C%7B%22description%22%3A%22Binance%20USDT%22%2C%22proName%22%3A%22BINANCE%3ABTCUSDT%22%7D%2C%7B%22description%22%3A%22BitFinex%20USDT%22%2C%22proName%22%3A%22BITFINEX%3ABTCUSD%22%7D%2C%7B%22description%22%3A%22BitFlyer%20JPY%22%2C%22proName%22%3A%22BITFLYER%3ABTCJPY%22%7D%5D%2C%22width%22%3A%22100%25%22%2C%22height%22%3A72%2C%22utm_source%22%3A%22coinfarm.online%22%2C%22utm_medium%22%3A%22widget%22%2C%22utm_campaign%22%3A%22tickers%22%7D', {timeout: 0, waitUntil: 'networkidle0'})
    const wait = await page.waitForSelector('.tv-ticker-item-change__last')
    const eVal = await page.evaluate( () => document.querySelectorAll('.tv-ticker-item-change__last')[0].innerText )

    console.log ( parseFloat( eVal ) )

    const exit = await browser.close()
})()
Edi Imanto
  • 2,119
  • 1
  • 11
  • 17