javascript - 使用 puppeteer 进行 JS 网页抓取,出现此错误 : (node:12121) UnhandledPromiseRejectionWarning: TypeError: src. jsonValue 不是函数

标签 javascript json web web-scraping puppeteer

大家好, 我正在尝试使用 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 书籍的封面图片。收到相同的错误消息。

感谢您的回复!!

最佳答案

  1. getProperty 返回一个 Promise,因此应该是 await el.getProperty("src")
  2. 始终确保等待该元素出现在页面上。使用 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/

相关文章:

javascript - 选择 ul li 层次结构中 td 的所有 innerHTML

MSBuild:构建asp.net 4.0网站:.metaproj -files

javascript - 如何将新项目推送到对象内部的数组

php - 当我包含特殊字符串时,Wordpress 不会解析 JSON

json - 如何将 Django 查询集输出为 JSON?

java - 如何在 apache web 服务器中配置 tomcat java 应用程序?

c# - 如何托管 Web 服务以便连接到同一网络的其他客户端可以访问

javascript - 将 JQuery 函数移至函数内部,然后从多个函数访问它

javascript - less 没有在 head 中定义

javascript - jquery 显示单击的元素的隐藏加载程序