phantomjs 的使用出现不稳定现象, 有时候有结果有时候没结果

DigitalMonk 2014-10-28 10:08:05
命令:phantomjs --output-encoding=gbk test.js http://webscan.360.cn/index/checkwebsite
有时候能够正常返回 diaoyu_sec 的值, 但有个别时候却找不到diaoyu_sec的值 , 返回结果提示: 'null' is not an object
请问是不是因为网速问题导致这种现象呢? 有什么好的补救解决方法?


test.js文件如下:
var page = require('webpage').create(),
system = require('system'),
address;
if (system.args.length === 1)
{
phantom.exit(1);
}
else
{
address = system.args[1];
page.open(address, function (status)
{

var ua = page.evaluate(function () {
return document.getElementById('diaoyu_sec').textContent;
});
console.log(ua);
phantom.exit();
}
);
}
...全文
299 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Go 旅城通票 2014-10-29
  • 打赏
  • 举报
回复
引用 2 楼 DigitalMonk 的回复:
谢,期待更明细的解答.
http://webscan.360.cn/index/checkwebsite这个页面原本就没有返回diaoyu_sec对象吧,要检查指定的网站才会返回,缺少了url参数 phantomjs --output-encoding=gbk test.js http://webscan.360.cn/index/checkwebsite?url=www.coding123.net 如果加了url参数还是没有返回,那么就是网络或者网站设置了请求限制什么的,和phantomjs无关,你将返回的status和内容输入到文件中就知道了
    page.open(address, function (status) {
        var fs = require('fs');
        var filename = new Date().getTime() + '.txt';
        fs.write(filename,status+"\r\n\r\n"+ page.content, 'w');////////

        var ua = page.evaluate(function () {
            return document.getElementById('diaoyu_sec').textContent;
        });
        console.log(filename);
        console.log(ua); //如果返回空自己找对应的文件检查返回了什么内容
        phantom.exit();
    });
Go 旅城通票 2014-10-28
  • 打赏
  • 举报
回复
没用过,不过猜测是网路出问题导致http://webscan.360.cn/index/checkwebsite返回错误或者这个页面加了访问控制什么的,返回了其他内容导致找不到对象 自己将下载的内容打印出来看看
DigitalMonk 2014-10-28
  • 打赏
  • 举报
回复
谢,期待更明细的解答.

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧