apify - 在 Apify 中,如何从嵌套函数中登录到控制台?
问题描述
从 Apify 示例文档中,我可以看到,如果您使用console.log()
within handlePageFunction
,它会直接记录到终端控制台。Apify utils 也是如此log
。例如:
handlePageFunction: async ({ request, page }) => {
console.log('This is logged to the terminal console');
log.info('So is this.')
...
}
但是,当我在handlePageFunction 中定义自己的函数时,我无法将任何内容记录到终端控制台。我希望 console.log 会转到浏览器控制台,但在 Apify 中,浏览器窗口会迅速出现和消失。(也可以使用根本不显示浏览器的 apify-cli。)
handlePageFunction: async ({ request, page }) => {
...
const myFunction = () => {
console.log('This disappears into the ether.');
log.info('This causes the script to fail with error, "log is not defined"');
}
myFunction();
}
如何在嵌套的 javascript 函数中使用 Apify utils.log?
解决方案
如果您page.evaluate
在浏览器上下文中评估的 a 或任何其他函数中使用定义的函数,则会在浏览console.log
器控制台中输出。如果您想让浏览器日志显示在 Node.js 中,这应该适用于简单的日志记录。-
page.on('console', msg => console.log('PAGE LOG:', msg.text));
推荐阅读
- chart.js - chart.js 标签文本字体大小和数字文本字体大小太小
- python - 从 matlab 移植到 python 的遗传算法似乎没有进化
- java - 尝试在 Android Studio 中的空对象引用上调用接口方法“java.lang.Object java.util.List.get(int)”
- python - 我必须创建嵌套循环还是可以使用 try 函数来创建?
- php - 如何将(合并的)下拉菜单值发送到数据库?
- javascript - JavaScript 中的对象创建和新对象
- css - 材质选择边框的颜色如何改变?
- android - 将日志写入流内的文件并在协程内收集
- python - 为什么这个 DataFrame 没有做我想做的事情?
- python-3.x - 将图像转换为图标会出错