how can I upload my website with a webscrapper in it and run?

Viewed 27

first of all, i dont know much about services, web apps and such, i've been studying js for the past 40 days and i managed to build a simple web scrapper that scrapes data about us dolar and indexes, saves it in a json and in my html i have a js that treats that json information, do some math and show it on the browser.

the thing is, i need the index.js file to run everyday at 8 a.m, i already uploaded a website, static website and i just put the files there, basicay img, style, index and js, but since this has index.js, node files and i need to run the index automaticaly i have no idea how to go about it.

const puppeteer = require('puppeteer');
const fs = require('fs');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://br.advfn.com/investimentos/futuros/di-depositos-interfinanceiros/cotacoes',{
    waitUntill: 'load',
    timeout: 0
  });
  
  const textNode = await page.evaluate(()=>{
    const nodeText = document.querySelector(".even.first").innerText;
    const text = [nodeText];
    
    return text
  });

  fs.writeFile('arreglo2.json', JSON.stringify(textNode), err =>{
    if (err) throw new Error ('algo deu errado')
      console.log('deu certo')
  })

})();

//**********************pegar o DX**************************************************/
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://br.tradingview.com/symbols/TVC-DXY/',{
    waitUntill: 'load',
    timeout: 0
  });
  

  const textNode = await page.evaluate(()=>{
    const nodeText = document.querySelector(".js-quote-ticker.tv-site-table__row.tv-widget-watch-list__row:nth-child(2)").children[1].children[1].children[0].innerHTML;
    const text = [nodeText];
    
    return text
  });

  fs.writeFile('arreglo.json', JSON.stringify(textNode), err =>{
    if (err) throw new Error ('algo deu errado')
      console.log('deu certo')
  })

})();

/**********vai pegar a cotação do fechamento do colar */

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://br.advfn.com/bolsa-de-valores/fx/USDBRL/cotacao',{
    waitUntill: 'load',
    timeout: 0
  });
  

  const textNode = await page.evaluate(()=>{
    const nodeText = document.querySelector(".qs-current-price").innerText;
    const text = [nodeText];
    
    return text
  });

  fs.writeFile('cotacaoFechamento.json', JSON.stringify(textNode), err =>{
    if (err) throw new Error ('algo deu errado')
      console.log('deu certo')
  })

})();

the code above is my index.js the one that gets the info i need and save it to a json.

today when i want to test the file i go to terminal and type node index.js, wait till its ok and then refresh my browser.

ah, i had to download some xampp server and saved my solution in the the folder where it could be accessed thru localhost (when i saved in normal c:// it didnt scrape at all, some CORS-related error). thank you all in advance.

1 Answers
Related