获得不同的结果Puppeteer和startsWith()

时间:2020-10-30 10:03:39

标签: javascript arrays puppeteer

我正在尝试使用Puppeteer阅读网站的链接,并使用startsWith()函数过滤结果。

问题在于,根据startsWith()是将参数作为变量获取还是直接将其作为字符串获取值,我得到了不同的结果。

示例:

const root = 'https://tiempone.com';

const browser = await puppeteer.launch({headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox']});

const page = await browser.newPage();

await page.goto(root, {waitUntil: 'networkidle0'});

// root and string are equal -> 'https://tiempone.com'
let urlsToVisit_1  = await page.$$eval('a', links => links.map(link => link.href).filter(link => link.startsWith(root)));  
let urlsToVisit_2  = await page.$$eval('a', links => links.map(link => link.href).filter(link => link.startsWith('https://tiempone.com'))); 

console.log(urlsToVisit_1); 
// []

console.log(urlsToVisit_2);  
// ['https://tiempone.com/', 'https://tiempone.com/projects', ..., 'https://tiempone.com/privacy']

我希望urlsToVisit_1urlsToVisit_2相等。

为什么我没有得到相同的结果?

1 个答案:

答案 0 :(得分:1)

root在Node.js上下文中声明,而page.$$eval()中的函数在浏览器上下文中执行。您需要显式传输变量:

let urlsToVisit_1  = await page.$$eval(
  'a',
  (links, root) => links.map(link => link.href).filter(link => link.startsWith(root)),
  root,
);