被标记为异步调用的问题,但是调用在最后几行代码之前完成,不了解这是怎么引起问题的?
我是Javascript的初学者,尝试为不和谐的机器人编写代码。我遇到了另一个花了几天时间才能解决的问题。请有人能告诉我为什么我的代码会抛出此JSON未定义错误吗?
function search(msg) {
const puppeteer = require('puppeteer');
let scrape = async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto(msg);
const result = await page.evaluate(() => {
let data = []; // Create an empty array that will store our data
let ingredients = document.querySelector("ul li").innerText; // Select the ingredients
let level = document.getElementsByTagName("p").innerText; // Select the level
data.push({ingredients, level}); // Push an object with the data onto our array
return data; // Return our data array
});
browser.close();
return result; // Return the data
};
scrape().then((value) => {
return value; // Success!
});
};
var prep5 = search(prep4); //prep4 is a variable string providing a URL, testing with "https://ffxiv.consolegameswiki.com/wiki/Crab_Oil"
var obj = JSON.parse(prep5);
console.log(obj.ingredients + ' and ' + obj.level);
我看到操纵up的人成功打开了网页,然后在完成加载后自行关闭,并且在这种情况下,希望控制台看到“ 2 Water Shard,2 Megalocrab Leg和Level 26 Alchemist”输出,但是我得到的只是
“ SyntaxError:JSON中意外的令牌u在JSON.parse()的位置0处”
编辑:快速链接到参考页面-https://ffxiv.consolegameswiki.com/wiki/Crab_Oil