-6ren"> -在 Puppeteer 上运行,全部更新。 预期的过程是访问网站,其中 url 是 url/{search item} 并运行搜索名称列表。然后对于每个搜索项 --> 搜索页面,获取每个列表的名称、价-6ren">
gpt4 book ai didi

javascript - 错误: failed to find element matching selector for 转载 作者:行者123 更新时间:2023-12-01 01:03:00 36 4

gpt4 key购买 nike

在 Puppeteer 上运行,全部更新。

预期的过程是访问网站,其中 url 是 url/{search item} 并运行搜索名称列表。然后对于每个搜索项 --> 搜索页面,获取每个列表的名称、价格和图像 url。现在出现错误,找不到选择器。感谢对此的任何帮助,非常感谢!

网站数据布局如下:

<div class="items-box-content">
<section class="items-box">
<a href="https://listingurl">
<figure class="items-box-photo">
<img data-src="https://imageurl.jpg" class=" lazyloaded" src="https://imageurl.jpg">
</figure>
<div class="items-box-main">
<h3 class="items-box-name"> listing name </h3>

<div class="items-box-figure">
<div class="items-price font-4"> $29.95 </div> // item's price
</h3>
</div>

我现在拥有的是(引发错误):

const puppeteer = require('puppeteer');
const searches = ["a", "b", "c"]; //appended to url
(async () => {
const browser = await puppeteer.launch({ headless: false });
let results =[];
for (const search of searches) {
try {
page = await browser.newPage();
await page.goto(`https://weburl/?keyword=${search}`);
await page.evaluate(() => { document.querySelector('div[class*="items-box"]').scrollIntoView();});
let elements = await page.$$('div[class*="items-box"]');
for (let element of elements) {
let listImg = await element.$eval(('img[class="items-box-photo]'), img => img.getAttribute('src'));
let listTitle = await element.$eval(('d[class="items-box-main"] > h[class="items-box-name"]'), node => node.innerText.trim());
let listPrice = await element.$eval(('d[class="items-box-figure"] > d[class="items-price"]'), node => node.innerText.trim());
let listUrl = await element.$eval(('d[class="items-box-content"] > a[class*="items-box"]'), node => node.getAttribute('href'));

results.push({
listImg,
listTitle,
listPrice,
listUrl
})
return results;
}
} finally {
await page.close
}
}
})();

抛出的错误是

(node:5168) UnhandledPromiseRejectionWarning: Error: Error: failed to find element matching selector "img[class="items-box-photo]"

最佳答案

我通过测试/调试更新了您的代码。

const puppeteer = require('puppeteer');

const searches = ["a"];

(async () => {

const browser = await puppeteer.launch({ headless: false });

function delay(timeout) {
return new Promise((resolve) => {
setTimeout(resolve, timeout);
});
}

let results = [];

for (const search of searches) {

try {
page = await browser.newPage();
await page.goto(`https:url/`);

await page.evaluate(() => { document.querySelector('section[class*="items-box"]').scrollIntoView(); });

let elements = await page.$$('section[class*="items-box"]');
console.log(elements.length)
console.log('wait 6 seconds')
await delay(6000);

for (let element of elements) {
// await delay(6000);

let listImg = await element.$eval(('img'), img => img.getAttribute('src'));
let listTitle = await element.$eval(('h3[class="items-box-name font-2"]'), node => node.innerText.trim());
let listPrice = await element.$eval(('div[class="items-box-price font-5"]'), node => node.innerText.trim());
let listUrl = await element.$eval(('div[class="items-box-content clearfix"] a'), node => node.getAttribute('href'));

results.push({
listImg,
listTitle,
listPrice,
listUrl
});
}

debugger;

} catch (error) {
console.log(error)
} finally {
//await page.close
await browser.close
}
}
console.log(results)
return results;
})();
<小时/>

更新内容:
1. for循环中的返回结果

for(){
return result;
}

=>

for(){

}
return result;
  • 更新了querySelector
  • section[class*="items-box"]

    img // There is only one img tags in "element"

    h3[class="items-box-name font-2"] // removed outer 'element'

    div[class="items-box-figure"] > div[class="items-price font-4"]

    div[class="items-box-price font-5 // updated class name? on my side

    items-box-price

    div[class="items-box-content clearfix"] a
  • 更新 sleep 持续时间 6 秒,这是相对网络速度(网络加载持续时间)。

  • 尝试 捕获 终于
    catch 帮助您处理下一步,尽管一步崩溃。

  • 关于javascript - 错误: failed to find element matching selector for <img data-src="url>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55887523/

    36 4 0
    行者123
    个人简介

    我是一名优秀的程序员,十分优秀!

    滴滴打车优惠券免费领取
    滴滴打车优惠券
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com