puppeteer:从 NodeList 获取 HTML? [英] puppeteer: Getting HTML from NodeList?
本文介绍了puppeteer:从 NodeList 获取 HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从代码中得到一个包含 30 个项目的列表:
I'm getting a list of 30 items from the code:
const boxes = await page.evaluate(() => {
return document.querySelectorAll("DIV.a-row.dealContainer.dealTile")
})
console.log(boxes);
The result
{ '0': {},
'1': {},
'2': {},
....
'28': {},
'29': {} }
我需要查看元素的 html.
I have the need to see the html of the elements.
但是我尝试过的 boxes
的每个属性都只是 undefined
.我尝试了 length
、innerHTML
、'innerText` 和其他一些.
But every property I tried of boxes
is simply undefined
. I tried length
, innerHTML
, 'innerText` and some other.
我确信盒子确实包含一些东西,因为在我开始浏览"页面内容之前,puppeteer 的屏幕截图显示了内容
I am sure of box really containing something because puppeteer's screenshot shows the content before I start to 'browse' the content of the page
我做错了什么?
推荐答案
有多种方法可以做到这一点:
There are multiple ways to do this:
- 使用
page.$$eval
执行选择器并一步返回结果. - 使用
page.evaluate
查询元素后获取属性.
page.$$eval
const htmls = await page.$$eval('selector', el => el.innerHTML);
page.evaluate
const singleBox = boxes[0];
const html = await page.evaluate(el => el.innerHTML, singleBox);
这篇关于puppeteer:从 NodeList 获取 HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文