【问题标题】:CasperJS cannot obtain IP without other useless data没有其他无用数据,CasperJS 无法获取 IP
【发布时间】:2015-03-23 23:15:10
【问题描述】:

我现在更新了 CasperJS 脚本,见下文

var casper = require('casper').create();

casper.start('http://whatismyipaddress.com/', function() {
    if (this.exists('#section_left > div:nth-child(2)')) {
        var data = this.getElementInfo('#section_left > div:nth-child(2)');
        console.log(JSON.stringify(data.text));
    }
});

casper.run();

如何从下面得到的结果中删除 "\n\t

"\n\n23.221.147.202\n\n\t\t\t\t\t"

【问题讨论】:

    标签: javascript string casperjs stringify


    【解决方案1】:

    data.text 是一个字符串。对字符串进行字符串化会导致转义空格和添加引号。不要对字符串进行字符串化:

    console.log(data.text);
    

    如果你真的想去掉空格,有字符串函数trim()

    console.log(data.text.trim());
    

    【讨论】:

    • 更新答案
    猜你喜欢
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 2014-07-12
    相关资源
    最近更新 更多