【问题标题】:I'm trying to grab the value after the tag and the value is not in the tag in puppetter我正在尝试获取标记后的值,并且该值不在 puppetter 的标记中
【发布时间】:2021-06-17 07:10:32
【问题描述】:

我正在尝试用 puppeteer 从 this webpage 中获取 this color。但我无法正确刮取该颜色。

const productColor_Selector = '.lang1 p:nth-of-type(1)'
const productColor = await page.$eval(productColor_Selector, e => e.textContent)

console.log("color", productColor)

let colorFilter = productColor;
let splitColor = colorFilter.split('Color:');
let colorProduct = splitColor[1].trim().split(' ')[1];

console.log("Final Color:", colorProduct)

由于运行我的代码,有时我得到颜色,但它的格式不正确,有时它返回为undefined:see the screenshot

【问题讨论】:

  • 不要将相关内容发布为图片 -> How do I ask a good question?
  • 阿里,请不要忘记为有帮助的答案投票,如果有答案,请接受答案作为解决方案。

标签: javascript web-scraping puppeteer


【解决方案1】:

你可以试试这样的:

const colorProduct = await page.evaluate(
  () => document.querySelectorAll('.lang1 p:nth-of-type(1) strong')[1]
          .nextSibling.nodeValue
);

【讨论】:

  • 非常感谢先生!它的作品非常好:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多