如何过滤page.$$eval的结果?筛选含/product/和/c/的href
/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

