我在网站上运行以下代码:
let details = await page.$$eval(
"#od-subtotals .a-row:not(.a-spacing-mini)",
nodes =>
nodes.map(async n => {
let heading = await n.$eval(".a-text-left span", nn => nn.innerText);
let amount = await n.$eval(".a-text-right span", nn => nn.innerText);
return { heading: heading, amount: amount };
})
);
$$eval
方法工作正常,如果我仅在$$eval(sel, nodes => nodes.map(n => n.innerText)
上运行地图,则会收到一个数组。
现在,我正在尝试进一步分离节点。当我read the docs时说:
page.$$eval(selector, pageFunction[, ...args])
selector <string> A selector to query page for
pageFunction <function(Array<Element>)> Function to be evaluated in browser context
因此,我的想法是,我在运行.$eval
方法的页面上循环浏览具有以下特征的元素:
elementHandle.$eval(selector, pageFunction[, ...args])
selector <string> A selector to query page for
pageFunction <function(Element)> Function to be evaluated in browser context
我确实收到了上述错误:
Uncaught (in promise) TypeError: n.$eval is not a function
at __puppeteer_evaluation_script__:7
at Array.map (<anonymous>)
at VM163 __puppeteer_evaluation_script__:2
(anonymous) @ __puppeteer_evaluation_script__:7
(anonymous) @ __puppeteer_evaluation_script__:2
答案 0 :(得分:0)
因此,似乎从semester_1 tinyint
semester_2 tinyint
trimester_1 tinyint
trimester_2 tinyint
trimester_3 tinyint
term_1 tinyint
term_2 tinyint
term_3 tinyint
term_4 tinyint
year_end tinyint
返回的Element实际上不是ElementHandle,因此不能与.$$eval
函数一起使用。我正在浏览文档,似乎也没有办法将它们转换。
代码需要更改为:
$eval
大概还有一种方法,我们可以使用let details = await page.$$eval(
"#od-subtotals .a-row:not(.a-spacing-mini)",
nodes =>
nodes.map(async n => {
let heading = n.querySelector(".a-text-left span").innerText;
let amount = n.querySelector(".a-text-right span").innerText;
return { heading: heading, amount: amount };
})
);
然后迭代ElementHandle数组,然后使用.$$
。