大家好, 我正在尝试使用 puppeteer 抓取某些内容,并且由于某种原因收到以下错误消息。
(node:12121) UnhandledPromiseRejectionWarning: TypeError: src.jsonValue is not a function
at scrapeProduct (/Users/jesse/Documents/scraper/scrapers.js:12:28)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:12121) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12121) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
有什么想法吗?
我对节点非常陌生,这可能是我的问题。我还没有创建太多代码,我只是想先抓取一张图像进行测试。这是我到目前为止所拥有的。
const puppeteer = require("puppeteer");
async function scrapeProduct(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const [el] = await page.$x(
'//*[@id="grandexchange"]/div/div[2]/main/div[2]/div[1]/img'
);
const src = el.getProperty("src");
const srcTxt = await src.jsonValue();
console.log({ srcTxt });
await browser.close();
}
scrapeProduct(
"http://services.runescape.com/m=itemdb_rs/Uncut+sapphire/viewitem?obj=1623"
);
这是我的第二次尝试。我还尝试提取 Amazon.com 书籍的封面图片。收到相同的错误消息。
感谢您的回复!!
最佳答案
getProperty
返回一个 Promise,因此应该是await el.getProperty("src")
- 始终确保等待该元素出现在页面上。使用
await page.waitForXPath()
作为 xpath,使用await page.waitForSelector()
作为选择器。
const puppeteer = require('puppeteer');
async function scrapeProduct(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
await page.waitForXPath('//*[@id="grandexchange"]/div/div[2]/main/div[2]/div[1]/img');
const [el] = await page.$x(
'//*[@id="grandexchange"]/div/div[2]/main/div[2]/div[1]/img'
);
const src = await el.getProperty('src');
const srcTxt = await src.jsonValue();
console.log({ srcTxt });
await browser.close();
}
scrapeProduct(
"http://services.runescape.com/m=itemdb_rs/Uncut+sapphire/viewitem?obj=1623"
);
关于javascript - 使用 puppeteer 进行 JS 网页抓取,出现此错误 : (node:12121) UnhandledPromiseRejectionWarning: TypeError: src. jsonValue 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59692500/