1

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>
Barmar
  • 741,623
  • 53
  • 500
  • 612
jasonalan
  • 11
  • 3
  • Not sure if it's related to the problem, but you have two calls to `runScript()`. – Barmar Sep 13 '21 at 20:48
  • Do these answer your question? [How to make Puppeteer work with a ReactJS application on the client-side](https://stackoverflow.com/questions/55031823/how-to-make-puppeteer-work-with-a-reactjs-application-on-the-client-side); [Include Puppeteer (javascript) code inside of Static HTML](https://stackoverflow.com/questions/63512859/include-puppeteer-javascript-code-inside-of-static-html) it is a very frequent question here on SO, the answer is always: you cannot call NodeJs code from client-side code (HTML). You will need to make it and endpoint that can be called from client-side. – theDavidBarton Sep 14 '21 at 08:31

0 Answers0