如何在PhantomJS中保存XML GET响应到文件

How to save XML GET response to file in PhantomJS

本文关键字:GET 响应 文件 XML 保存 PhantomJS      更新时间:2023-09-26

我想保存一个GET响应到一个文件。这是我的代码:

function(){
    var page=require('webpage').create();
    var callback=function(status){
        if (status=='success'){  
            page.render('pic1.png');   
            var fs = require('fs');
            fs.write('reppo.xml',page.content,'w')
            console.log('Got report...');
        }else{
            console.log('Failed to load.');
        }
    };
    var url =  "https://10.84.163.146/event_stream/events_in_xml?events=137,138&arow=902&noxsl=y&nonmal=y";
    page.open(url,callback);   
},

我期望XML文档作为答案。当我把链接直接在我的浏览器,一切工作正常,但当我执行脚本,我得到一个文件,其中包含以下代码:

<html xmlns="http://www.w3.org/1999/xhtml"><body><parsererror style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>This page contains the following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 1 at column 0: Encoding error</div><h3>Below is a rendering of the page up to the first error.</h3></parsererror></body></html>

任何想法?

尝试更改您的用户代理。我知道在PHP中,如果我不这样做,一些网站会阻止访问。在启动网页后立即添加此代码。page.settings.userAgent = 'Chrome/28.0.1500.71';