我想从亚马逊上提取有关产品的问题和答案。但是我从我尝试过的代码中仅获得数组中的1个元素。
我尝试在实际的浏览器中运行querySelectorAll(),但它会正确返回9个元素。
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
const pageURL = "https://www.amazon.in/Espoir-Analog-Blue-Dial-Watch-ESP12457/dp/B07417987C/ref=sr_1_1?s=watches&rps=1&ie=UTF8&qid=1546787547&sr=1-1&refinements=p_98%3A10440597031%2Cp_n_material_browse%3A1480914031|1480915031";
await page.goto(pageURL, { waitUntil: "networkidle2" });
const QAs = await page.evaluate(() => {
let elements = Array.from(document.querySelectorAll("div.a-fixed-left-grid-col .a-col-right"));
let links = elements.map(element => {
return element.innerText
})
return elements;
});
console.log("q=", QAs);
答案 0 :(得分:1)
您需要滚动到元素容器并等待Ajax请求完成并呈现QA
await page.evaluate(() => {
document.querySelector('#ask_lazy_load_div').scrollIntoView();
});
await page.waitForSelector(".askTopQandA", {timeout: 10000}); // 10 seconds
const QAs = await page.evaluate(() => {....