node.js - 如何将上下文 Node 添加到 puppeteer 中的 xpath 查询?

标签 node.js xpath puppeteer

我试图循环访问页面上的多个元素并在每个 div 中获取一些数据......但是它看起来不像 xpath page.$x(<query>)支持上下文 Node 。要么是我没有正确指定上下文 Node :

const puppeteer = require('puppeteer');

async function scrape(url) {
  console.log('scraping', url);
  const res = [];
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(url);

  const deals = await page.$x('//*[@class="a-row dealContainer dealTile"]/div/div[2]/div');
  // const dealEls = await page.evaluate((...deals) => {
  //   return deals.map(el => el);
  // }, ...deals);

  console.log(deals.length, 'found');

  for (let deal of deals) {
    const [imgEl] = await deal.$x('//*[@id="dealImage"]/div/div/div[1]/img');
    const src = await imgEl.getProperty('src');
    const img = await src.jsonValue();

    const [titleEl] = await deal.$x('//*[@id="dealTitle"]/span');
    const text = await titleEl.getProperty('textContent');
    const title = await text.jsonValue();

    const [priceEl] = await deal.$x('//*[@id="101_dealView_0"]/div/div[2]/div/div/div[3]/div[1]/span');
    const priceTxt = await priceEl.getProperty('textContent');
    const price = await priceTxt.jsonValue();

    //
    const [origPriceEl] = await deal.$x('//*[@id="101_dealView_0"]/div/div[2]/div/div/div[3]/div[2]/span[2]');
    const origTxt = await origPriceEl.getProperty('textContent');
    const origPrice = await origTxt.jsonValue();

    res.push({img, title: title.trim(), price, origPrice});
  }


  await browser.close();
  console.log(res);
}

scrape('https://www.amazon.com/gp/goldbox/ref=gbps_ftr_s-5_111b_prm_ISPM?gb_f_deals1=dealTypes:DEAL_OF_THE_DAY%252CBEST_DEAL%252CLIGHTNING_DEAL,sortOrder:BY_SCORE,dealStates:AVAILABLE%252CWAITLIST%252CWAITLISTFULL,includedAccessTypes:GIVEAWAY_DEAL,enforcedCategories:172282,discountRanges:10-25%252C25-50%252C50-70,minRating:3,primeEligibleOnly:true&pf_rd_p=18c48e87-0e4c-4250-8975-2c9a7587111b&pf_rd_s=slot-5&pf_rd_t=701&pf_rd_i=gb_main&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=8A7XXQ3N68HFWAMNKHZF&ie=UTF8')

最佳答案

要在另一个 Node 内部搜索,您可以使用 elementHandle.$x而不是page.$x 。引用文档:

The method evaluates the XPath expression relative to the elementHandle.

此外,您需要将 //* 更改为 /*,因为 //* 从根 Node 查询所有 Node 文档而不是使用给定的 Node 。

代码示例

这意味着,相应的代码部分应该如下所示:

const imgEl = await deal.$x('/*[@id="dealImage"]/div/div/div[1]/img');

关于node.js - 如何将上下文 Node 添加到 puppeteer 中的 xpath 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61570860/

相关文章:

xml - 使用 xpath 删除节点

xpath - Google Sheet importXml默认将时间值转换为十进制值

Puppeteer:在 page.evaluate 中使用函数

node.js - 单击基于 $eval 的 div 抛出 div click 不是 puppeteer 中的函数

windows - Bower 安装包 EPERM 重命名错误

javascript - 从 Neo4j 解析复杂的 JSON 结果

javascript - 非常简单的 Node.js 客户端在多次 http 请求后抛出错误 ENOBUFS

javascript - 导出函数中 Node.js 中响应正文的未定义值。

perl - XML::Twig xpath 栏

javascript - 使用 cookie 进行网络抓取验证?