【发布时间】:2023-03-16 02:40:01
【问题描述】:
我正在尝试从保存 HTML 数据的变量中抓取 HTML 数据。你可以看到我的注释,它们标有“evaluate 仅适用于不在 div 中的页面。有人能告诉我如何从包含 HTML 的变量中抓取信息吗?
是否有其他的抓取方法?
我也在forEach 循环中尝试过这个,但这导致了原始文档的第一个mealname。
let mealName = htmlOfOneProduct.document.querySelector("div.meal__description-texts.js-meal-description-text > span > span").innerText;
我的带注释的代码:
const puppeteer = require('puppeteer');
function run () {
return new Promise(async (resolve, reject) => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(" "); << Meal website
let urls = await page.evaluate(() => {
let results = [];
let items = document.querySelectorAll('div.meal__wrapper'); << Gets all the meals from a page
items.forEach((item) => {
let htmlOfOneProduct = item.innerHTML; << Gets the HTML of each meal
let mealName = htmlOfOne.evaluate(() => document.querySelector('meal-name').textContent); << Not working, should get the meal-name from the div.
results.push({
mealName: mealName
});
});
return results;
})
browser.close();
return resolve(urls);
} catch (e) {
return reject(e);
}
})
}
run().then(console.log).catch(console.error);
【问题讨论】:
标签: javascript node.js web-scraping google-chrome-devtools puppeteer