【问题标题】:Puppeteer : use second match with page.evaluatePuppeteer:使用第二场比赛与 page.evaluate
【发布时间】:2020-11-27 01:36:01
【问题描述】:

我正在使用 puppeteer 在线检索数据,但遇到了问题。

两个函数具有相同的名称并返回序列化对象,第一个返回一个空对象,但第二个包含我要定位的数据。

我的问题是,我怎样才能继续选择函数的第二次出现而不是第一次出现,它返回一个空对象。

谢谢。

我的代码:

const puppeteer = require('puppeteer');
const cheerio = require('cheerio');

const Variants = require('./variants.js');
const Feedback = require('./feedback.js');

async function Scraper(productId, feedbackLimit) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  /** Scrape page for details */
  await page.goto(`${productId}`);
  const data = (await page.evaluate()).match(/window.runParams = {"result/)

  const data = data.items

 await page.close();
 await browser.close();


  console.log(data);
  return data;
}

module.exports = Scraper;

网站源代码:

window.runParams = {};
window.runParams = {"resultCount":19449,"seoFeaturedSnippet":};

【问题讨论】:

  • 使用正则表达式匹配'runParams'函数,只取第二个匹配。搜索如何在正则表达式中使用“匹配项”。
  • 我试过了,但没有运气: const data = (await page.evaluate()).match(/window.runParams = {"result/)
  • 能否提供完整代码?我会尽力提供帮助
  • 我刚刚编辑了帖子以显示完整代码
  • 能否请您添加“数据”的完整值?

标签: node.js web-scraping puppeteer


【解决方案1】:

请试试这个,它应该可以工作。

const data = await page.content();
const regexp = /window.runParams/g;
const matches = string.matchAll(regexp);
    
for (const match of matches) {
  console.log(match);
  console.log(match.index)
}

【讨论】:

  • 呜呜,好用!,我能找到数据了;非常感谢!!
  • 很高兴为您提供帮助:)
猜你喜欢
  • 1970-01-01
  • 2019-08-26
  • 2022-11-23
  • 1970-01-01
  • 2015-03-11
  • 2022-01-08
  • 2019-09-03
  • 2020-11-11
  • 2012-03-27
相关资源
最近更新 更多