我正在向一个API发出请求,大多数时候我收到的响应都是有效的JSON,但是有时候JSON有点损坏,JavaScript无法处理它。
https://www.nike.com.br/ProductLookup/555088-041
当您将JSON粘贴到此网站时:
https://jsonformatter.curiousconcept.com/
网站上的JSON格式化程序意识到JSON已损坏,但设法修复得很好。
以下是我的代码片段,其中出现了中断:
async function scraper(url) {
const browser = await puppeteer.launch({headless: false})
const page = await browser.newPage();
await page.waitFor(2000)
await page.goto(url);
const content = await page.content();
if (initialFlag) {
innerText = await page.evaluate(() => {
let wholeJson = JSON.parse(document.querySelector("body").innerText);
let status = wholeJson[0].status;
if (status === "REMOVED") {
return {status: status}
}
innerText = await page.evaluate(() => {
.
Error: Evaluation failed: SyntaxError: Unexpected token
in JSON at position 1831
at JSON.parse (<anonymous>)
at __puppeteer_evaluation_script__:2:34
at ExecutionContext._evaluateInternal (C:\Users\user\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:217:19)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async ExecutionContext.evaluate (C:\Users\user\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:106:16)
at async scraper (C:\Users\user\Documents\Node Projects\bergi.js:23:21)
at async main (C:\Users\user\Documents\Node Projects\bergi.js:77:27)