【发布时间】:2019-09-17 03:26:15
【问题描述】:
在 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
}
}
})();
抛出的错误是
(节点:5168)UnhandledPromiseRejectionWarning:错误:错误:未能 查找匹配选择器“img[class="items-box-photo]”的元素
【问题讨论】:
标签: javascript node.js xpath puppeteer