You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何过滤page.$$eval的结果?筛选含/product/和/c/的href

How to Filter hrefs Containing Both /product/ and /c/

Got it! You have two straightforward ways to tweak your existing Puppeteer code to filter for hrefs that include both /product/ and /c/. Let me break them down for you:

1. Filter Directly in $$eval (Most Efficient)

You can combine the mapping and filtering steps right inside the $$eval callback to avoid storing all hrefs first. This is cleaner if you only need the filtered results:

const filteredHrefs = await page.$$eval('a', as => 
  as.map(a => a.href)
    .filter(href => href.includes('/product/') && href.includes('/c/'))
);

The filter method checks each href to see if it contains both substrings using the && operator—only hrefs that pass both checks get kept.

2. Get All hrefs First, Then Filter

If you need to keep the full list of hrefs for other purposes later, split the steps into two parts:

// First, fetch all hrefs like you originally did
const allHrefs = await page.$$eval('a', as => as.map(a => a.href));

// Then filter down to the ones you need
const filteredHrefs = allHrefs.filter(href => 
  href.includes('/product/') && href.includes('/c/')
);

Bonus: Case-Insensitive Filtering (If Needed)

If your target URLs might have mixed case (e.g., /Product/ or /C/), convert each href to lowercase before checking to make the filter more robust:

const filteredHrefs = await page.$$eval('a', as => 
  as.map(a => a.href)
    .filter(href => {
      const lowerHref = href.toLowerCase();
      return lowerHref.includes('/product/') && lowerHref.includes('/c/');
    })
);

内容的提问来源于stack exchange,提问作者Hatem Husam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:10:06