【发布时间】:2018-07-15 10:26:49
【问题描述】:
我正在使用 Puppeteer 1.6.0 版解析一个 html 表格
// inside the rowMarket variable I store all the rows of a table
rowMarket = await.page.$$('#searchTextResults > tbody > tr');
现在我想遍历所有这些,并为每一行获取一些 td 列的文本。
如果我使用以下代码,一切正常。
for(i=0<rowMarket.length;i++){
nameComponent = await rowMarket[i].$('td:nth-child(1) > a');
iT = await nameComponent .getProperty('innerText');
json = await iT.jsonValue();
otherComponent = await rowMarket[i].$(' ... ');
// ... I repeat the same stuff for every column.
}
为了重用一些代码而不是大量复制和粘贴,我定义了下一个函数
async function getContent(element){
innerText = await element.getProperty('innerText');
json = await innerText.jsonValue();
return json;
}
所以我可以用这种方式重构之前的代码
for(i=0<rowMarket.length;i++){
nameComponent = await rowMarket[i].$('td:nth-child(1) > a');
nameText = getContent(nameComponent);
otherComponent = await rowMarket[i].$(' ... ');
otherText = getContent(otherComponent);
// ...
}
但在深入研究文档时,我发现了 $eval function,这似乎是我尝试手工操作的绝佳组合。
接下来我会重构我的代码。我认为它非常干净紧凑。
for(i=0<rowMarket.length;i++){
nameText = await rowMarket[i].$eval('td:nth-child(1) > a', getContent);
otherText = await rowMarket[i].$eval(' ...', getContent);
// ...
}
但我收到下一个错误
(node:8056) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: elemento.getProperty is not a function
at dentroElemento (__puppeteer_evaluation_script__:2:30)
at ExecutionContext.evaluateHandle (c:\webscraping\node_modules\puppeteer\lib\ExecutionContext.js:97:13)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
我真的不明白这个错误,因为如果在“独立”模式下调用该函数可以正常工作。
我也试过这个
for(i=0<rowMarket.length;i++){
nameText = await rowMarket[i].$eval('td:nth-child(1) > a', e => console.log('hello?'));
}
但是 hello 字符串永远不会登录到控制台。所以我认为问题是没有调用 pageFunction 函数。或者我的代码可能有问题。
【问题讨论】:
标签: javascript puppeteer