Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
Javascript-Resolve promise to HTML

I'm attempting to write a website with javascript that scrapes data off of another web site using puppeteer. I am able to print the value to a console, but nothing happens when I attempt to set the result to an html element. Is there something wrong with my code? The HTML code that calls it is below the javascript code.

function sleep(miliseconds) {
  var currentTime = new Date().getTime();

  while (currentTime + miliseconds >= new Date().getTime()) {}
}

async function scrapeProduct(url) {
  const puppeteer = require('puppeteer');
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(url);
  sleep(1000);
  const [el] = await page.$x('//*[@id="resultTable"]/tbody/tr[1]/td[2]/div/div[1]/font[1]');
  const txt = await el.getProperty('innerHTML');
  const rawTxt = await txt.jsonValue();
  browser.close();
  return rawTxt;
}

function runScript() {
  scrapeProduct('https://www.purpleair.com/sensorlist?key=HIGZAD9XGVDEMVVZ&show=37253')
    .then(function(result) {
      //console.log(result); 
      document.getElementById("p1").innerHTML = result;
    });
}
runScript()
<html>

<head>
  <script type="text/javascript" src="purpleAir.js"></script>
</head>

<body>

  <title>Test</title>
  <h1></h1>
  <p id="p1">Hello World!</p>

  <script>
    runScript()
  </script>

  <form>
  </form>
</body>

</html>

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda