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>