我正在尝试使用以下代码在重复循环中检查qvc.com上当前正在播放的项目是什么,但是在“ const results = await ...”行上显示“ await仅在异步函数中有效”。
这是我的代码:
(async () => {
// Init
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.qvc.com/content/iroa.qvc.eastern.html');
// Selectors
const current_item_selector = '.galleryItem:first-of-type a';
// Functions
setInterval(function() { // Repeat every 5s
const results = await page.$(current_item_selector);
const item = await results.evaluate(element => element.title);
console.log(item);
}, 5000);
})();
更新:setTimeout应该是setInterval,这是我的错误,复制/粘贴错误。我在代码块中对此进行了更新,谢谢那些指出的人。
内部的函数也setInterval
需要异步:
// Functions
setInterval(async function() { // Repeat every 5s
const results = await page.$(current_item_selector);
const item = await results.evaluate(element => element.title);
console.log(item);
}, 5000);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句