Javascript CasperJS无法在没有其他无用数据的情况下获取IP
我现在已经更新了CasperJS脚本,请参见下文Javascript CasperJS无法在没有其他无用数据的情况下获取IP,javascript,string,casperjs,stringify,Javascript,String,Casperjs,Stringify,我现在已经更新了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
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"
data.text
是单个字符串。字符串化会导致转义空白和添加引号。不要字符串化:
console.log(data.text);
如果确实要删除空白,则有字符串函数trim()
:
更新您的答案,我使用了您的答案,并根据需要对其进行了修改------console.log(data.text.trim());
console.log(data.text.trim());