我正在尝试使用node.js中的puppeteer来爬取数据。
目前,我正在编写一个脚本,用于在well.ca的特定部分中爬取所有数据。
现在,这是我正在尝试通过node.js实现的方法/逻辑:
1 - 转到网站的“药品健康”部分
2 - 使用dom选择器从.panel-body-content获取href数组,通过dom选择器panel-body-content a[href]来爬取子部分
3 - 通过for循环迭代每个链接(子部分)
4 - 对于每个子部分链接,通过.col-lg-5ths col-md-3 col-sm-4 col-xs-6 a[href]获取每个产品的href数组,其中class值为col-lg-5ths col-md-3 col-sm-4 col-xs-6
5 - 循环遍历子部分中的每个产品
6 - 爬取每个产品的数据
目前,我已经编写了上述大部分代码。
目前,我正在编写一个脚本,用于在well.ca的特定部分中爬取所有数据。
现在,这是我正在尝试通过node.js实现的方法/逻辑:
1 - 转到网站的“药品健康”部分
2 - 使用dom选择器从.panel-body-content获取href数组,通过dom选择器panel-body-content a[href]来爬取子部分
3 - 通过for循环迭代每个链接(子部分)
4 - 对于每个子部分链接,通过.col-lg-5ths col-md-3 col-sm-4 col-xs-6 a[href]获取每个产品的href数组,其中class值为col-lg-5ths col-md-3 col-sm-4 col-xs-6
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
和id product_grid_link
内的所有href的正确DOM选择器是什么?
如果我想添加一个后续循环,通过每个子节的产品href获取每个产品的信息,该怎么将其嵌入到代码中?
非常感谢任何帮助
.col-lg-5ths.col-md-3.col-sm-4.col-xs-6 a[href]
似乎会为每个产品产生两个链接。我怎样才能仅从这个类product_grid_link
中获取 href?非常感谢,vsemozhetbyt。 - TekkySet
结构没有forEach
或map
方法;2)即使你将Set
转换为数组,forEach
或map
也不适合使用await
。 - vsemozhebuty