.wait()梦魇和Puppeteer评估都未能找到ID

问题描述 投票:3回答:1

我试图在aliexpress中获得评论但由于某种原因wait()功能总是找不到#transction-feedback

因此,从技术上讲,如果您转到该链接并单击反馈,则会显示所有评论

.click确实有效,因为它点击了标签,但似乎#transction-feedback从未出现过。我尝试使用浏览器重新创建相同的内容并显示#transction-feedback

enter image description here

这是代码

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()

我该怎么办?

node.js web-scraping puppeteer nightmare
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.