javascript - puppeteer 获取 href 数组,然后遍历每个 href 和该页面上的 href
问题描述
我正在尝试通过 node.js 中的 puppeteer 抓取数据
目前,我正在寻找编写一个脚本,该脚本会在 well.ca 的某个部分中抓取所有数据
现在,这是我试图通过 node.js 实现的方法/逻辑
1 - 前往网站的医学健康部分
.panel-body-content
2 - 使用 dom 选择器通过 dom 选择器获取一组 hrefpanel-body-content a[href]
以抓取子部分
3 - 使用 for 循环遍历每个链接(小节)
col-lg-5ths col-md-3 col-sm-4 col-xs-6
4 对于每个小节链接,通过获取具有值的每个类的 href,为每个产品获取另一个 href 数组.col-lg-5ths col-md-3 col-sm-4 col-xs-6 a[href]
5 - 遍历小节内的每个产品
6 - 为每个产品抓取数据
目前,我已经编写了上述大部分代码:
const puppeteer = require('puppeteer');
const chromeOptions = {
headless: false,
defaultViewport: null,
};
(async function main() {
const browser = await puppeteer.launch(chromeOptions);
try {
const page = await browser.newPage();
await page.goto("https://well.ca/categories/medicine-health_2.html");
console.log("::::::: OPEN WELL ::::::::::");
// href attribute
const hrefs1 = await page.evaluate(
() => Array.from(
document.querySelectorAll('.panel-body-content a[href]'),
a => a.getAttribute('href')
)
);
console.log(hrefs1);
const urls = hrefs1
for (let i = 0; i < urls.length; i++) {
const url = urls[i];
await page.goto(url);
}
const hrefs2 = await page.evaluate(
() => Array.from(
document.querySelectorAll('.col-lg-5ths col-md-3 col-sm-4 col-xs-6 a[href]'),
a => a.getAttribute('href')
)
);
当我尝试为每个产品的每个 href 获取一个数组时,我在数组中什么也没有收到。
如何添加嵌套的 for 循环,以获取每个小节中每个产品的所有 href 数组,然后访问每个产品链接?
.col-lg-5ths col-md-3 col-sm-4 col-xs-6
什么是正确的 dom 选择器,用于获取具有 id的类中的所有 href product_grid_link
如果我想添加一个后续循环以通过每个小节中产品的 href 从每个产品中获取信息,我该如何将其嵌入到代码中?
任何帮助将非常感激
解决方案
似乎有些链接是重复的,所以最好收集最终页面的所有链接,对链接列表进行重复数据删除,然后刮掉最终页面。(您也可以将最终页面的链接保存在文件中以供以后使用。)该脚本收集了 5395 个链接(已删除)。
'use strict';
const puppeteer = require('puppeteer');
(async function main() {
try {
const browser = await puppeteer.launch({ headless: false, defaultViewport: null });
const [page] = await browser.pages();
await page.goto('https://well.ca/categories/medicine-health_2.html');
const hrefsCategoriesDeduped = new Set(await page.evaluate(
() => Array.from(
document.querySelectorAll('.panel-body-content a[href]'),
a => a.href
)
));
const hrefsPages = [];
for (const url of hrefsCategoriesDeduped) {
await page.goto(url);
hrefsPages.push(...await page.evaluate(
() => Array.from(
document.querySelectorAll('.col-lg-5ths.col-md-3.col-sm-4.col-xs-6 a[href]'),
a => a.href
)
));
}
const hrefsPagesDeduped = new Set(hrefsPages);
// hrefsPagesDeduped can be converted back to an array
// and saved in a JSON file now if needed.
for (const url of hrefsPagesDeduped) {
await page.goto(url);
// Scrape the page.
}
await browser.close();
} catch (err) {
console.error(err);
}
})();
推荐阅读
- wordpress - 如何在课堂上使用 $wpdb
- python - Python 多个条件与 if else 语句?[已解决的案例]
- google-chrome-extension - Chrome 扩展应用程序有时会丢失本地存储
- google-maps - Google 如何在不使用移动互联网数据计划的情况下跟踪我?
- docker - 如何向我的 docker buildx 构建器添加对 mips 的支持?
- javascript - 复数包含数组的对象键
- c++ - 不能给出一个前导零的数字来运行吗?
- javascript - iOS 仅在背景元素的 CSS 设置为 cursor:pointer 时检测 mouseleave
- javascript - 如何在滚动时将此 JQuery 转换为 Vanilla JS
- c# - 如何在 ASP.net 中引用 HTML 元素?