node.js - Puppeteer 返回以数字为键、空对象为值的对象
问题描述
所以,我正在研究一个网络爬虫,我需要返回一组链接,为此:
const puppeteer = require('puppeteer');
const URL = 'SOME_URL';
const SELECTOR = 'SOME_SELECTOR'
const app = async () => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(URL,{waitUntil: 'networkidle0'});
await page.waitForSelector(SELECTOR);
let content = await page.evaluate(()=> {
let episodes = document.querySelectorAll(SELECTOR);
return episodes;
})
console.log(content)
await browser.close()
}catch(err) {
console.log(err)
}
}
我把这个作为输出,
{
'0': {},
'1': {},
'2': {},
'3': {},
'4': {},
'5': {},
'6': {},
'7': {},
'8': {},
'9': {},
... so on
}
任何指针为什么会发生,我尝试使用其他选择器甚至其他 URL。
解决方案
Unfortunately, page.evaluate()
can only transfer serializable values (roughly, the values JSON can handle). As document.querySelectorAll()
returns collection of DOM elements which are not serializable (they contain methods and circular references), each element in the collection is replaced with an empty object. You need to return either serializable value (for example, an array of hrefs) or use something like page.$$(selector)
and ElementHandle
API.
推荐阅读
- python - 如何使用python中的方程填充多维数组
- python - pyInquirer 库列表中没有显示问题?
- php - 如何与 .htaccess 建立短连接?
- ruby-on-rails-6 - 如何在模块内定义控制器?
- javascript - Chrome 扩展程序:如何设置功能以在日期更改时执行
- javascript - OpenLayers - ol.Feature(s) 在初始页面加载时正确显示,但在页面加载完成后添加新 ol.Feature(s) 时显示不正确
- flask - 烧瓶应用程序端口拒绝ec2上的连接
- python-3.x - 我正在尝试在 Python 中制作“河内之塔”,但我将项目从列表“移动”到列表的方法似乎不起作用
- node.js - 这是一个什么样的对话框?如何使用 Puppeteer 与之交互?
- c - 如果传入的函数有参数,我如何屏蔽来自 makecontext 的警告?