我试图在速卖通中获得评论,但由于某些原因,wait()
函数始终找不到#transction-feedback
从技术上讲,如果您转到该链接并单击“反馈”,它将显示所有评论
.click确实可以工作,因为它单击了选项卡,但似乎从未显示#transction-feedback
。我尝试使用浏览器重新创建相同内容,并显示#transction-feedback
。
这是代码
const cheerio = require('cheerio')
const Nightmare = require('nightmare')
const nightmare = Nightmare({
show: true
})
const URL = 'https://www.aliexpress.com/item/Samsung-Earphones-EHS64-Headsets-With-Built-in-Microphone-3-5mm-In-Ear-Wired-Earphone-For-Smartphones/32854052487.html?spm=2114.search0103.3.1.51dd26c3CxZ3zZ&ws_ab_test=searchweb0_0,searchweb201602_8_10065_10068_319_10059_10884_317_10887_10696_321_322_10084_453_10083_454_10103_10618_10307_537_536,searchweb201603_50,ppcSwitch_0&algo_expid=5587ff37-5e3a-45a8-8206-7b182433a961-0&algo_pvid=5587ff37-5e3a-45a8-8206-7b182433a961'
nightmare
.goto(URL)
.click('li[data-trigger="feedback"]')
.wait('#transction-feedback')
.evaluate(() => {
return document.body.innerHTML
})
.end()
.then((result) => {
const $ = cheerio.load(result)
res.json($.html())
})
.catch(error => {
console.error('Search failed:', error)
})
我也尝试了Puppeteer,但没有用
const puppeteer = require('puppeteer');
const URL = 'https://www.aliexpress.com/item/Samsung-Earphones-EHS64-Headsets-With-Built-in-Microphone-3-5mm-In-Ear-Wired-Earphone-For-Smartphones/32854052487.html?spm=2114.search0103.3.1.51dd26c3CxZ3zZ&ws_ab_test=searchweb0_0,searchweb201602_8_10065_10068_319_10059_10884_317_10887_10696_321_322_10084_453_10083_454_10103_10618_10307_537_536,searchweb201603_50,ppcSwitch_0&algo_expid=5587ff37-5e3a-45a8-8206-7b182433a961-0&algo_pvid=5587ff37-5e3a-45a8-8206-7b182433a961'
async function testPupp() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(URL);
await page.evaluate(() => {
document.querySelector('li[data-trigger="feedback').click();
console.log(document.body.innerHTML)
});
await browser.close();
}
testPupp()
我该怎么办?
答案 0 :(得分:2)
问题是iframe:尝试使用此代码
const puppeteer = require("puppeteer");
const URL =
"https://www.aliexpress.com/item/Samsung-Earphones-EHS64-Headsets-With-Built-in-Microphone-3-5mm-In-Ear-Wired-Earphone-For-Smartphones/32854052487.html?spm=2114.search0103.3.1.51dd26c3CxZ3zZ&ws_ab_test=searchweb0_0,searchweb201602_8_10065_10068_319_10059_10884_317_10887_10696_321_322_10084_453_10083_454_10103_10618_10307_537_536,searchweb201603_50,ppcSwitch_0&algo_expid=5587ff37-5e3a-45a8-8206-7b182433a961-0&algo_pvid=5587ff37-5e3a-45a8-8206-7b182433a961";
async function testPupp() {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto(URL, { waitUntil: "networkidle2" });
// evade popup
await page.click("body");
await page.keyboard.press("Escape");
await page.click('li[data-trigger="feedback');
// wait for iframe to load
await page.waitFor(2000);
const frame = (await page.frames()).find(f => f.url().includes("feedback"));
await frame.waitFor("#transction-feedback");
await frame.waitFor(".feedback-item");
const els = await frame.$$(".feedback-item");
for (const el of els) {
const text = await (await el.getProperty("textContent")).jsonValue();
console.log(text.trim().replace(/\s\s+/g, " "));
}
await browser.close();
}
testPupp();