【问题标题】:NodeJS in Ubuntu won't print console.log() to the terminalUbuntu 中的 NodeJS 不会将 console.log() 打印到终端
【发布时间】:2019-11-16 08:02:59
【问题描述】:

我是编程新手,我正在尝试按照教程来抓取 Craigslist 并从那里获取信息,然后将其打印到控制台/然后文件。 console.log() 在带有 NodeJS 的 Windows 10 中工作,但它在 Ubuntu 中无法以某种方式工作。我见过人们使用函数来打印它,但我不知道在我的小项目中如何做到这一点。

这是我的文件内容:

let jquery = require('jquery')
let Nightmare = require('nightmare'),
    nightmare = Nightmare()


nightmare.goto('https://vancouver.craigslist.org/search/jjj?postedToday=1')


    .wait(3000)


    .evaluate(function() {
        let items = [];
        $('.result-title').each(function() {
            item = {}
            item["title"] = $(this).text()
            item["link"] = $(this).attr("href")
            items.push(item)
        })
        return items
    })
    .end()

    .then(function(items) {

        for (item in items) {
            console.log(items[item].title)
            console.log(items[item].link)
            console.log("\n")
        }
    });

有人可以帮忙吗?谢谢

【问题讨论】:

  • 是否应该登录到浏览器控制台并注意 ubuntu 终端
  • @ZadikiHassanOchola 如何将其打印到终端?

标签: javascript node.js ubuntu console.log nightmare


【解决方案1】:

如果您console.log 在脚本顶部有任何内容,您在终端上看到什么了吗?

例如:

console.log('foobar') // add this line

let jquery = require('jquery')
// etc

如果这样做,请在最终的 .then 回调中尝试 console.log(items)。你看到什么了吗?

此外,您可能希望将 for items in item 替换为 items.forEach 循环。

    items.forEach(item => {
      console.log(item.title);
      console.log(item.link);
      console.log("\n");
    });

【讨论】:

  • 感谢您的回答 Sajad。如果我在 .then 回调之外尝试它,它会显示“foobar”,它在回调函数中不起作用,第二个改进也不起作用。
猜你喜欢
  • 2014-08-14
  • 2023-03-19
  • 2011-10-26
  • 2023-01-03
  • 2017-09-26
  • 2020-01-21
  • 2013-06-27
  • 2013-09-14
  • 1970-01-01
相关资源
最近更新 更多