search

Home  >  Q&A  >  body text

node.js - PhantomJS 获取请求资源的 response.body

在 使用 PhantomJSwebpage 模块 打开 weibo.con 页面 有 XMLHttpRequest 资源请求
PhantomJS 能直接得到 该资源的 返回值吗 (response.body)

黄舟黄舟2779 days ago700

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-17 14:31:08

    You can listen to the event after "File Received". If the type is ajax, then you use ajax to call this url.
    Now we don’t use phantomjs to write scripts. We usually use casperjs, but the idea is the same.
    For code, please refer to:

    casper.on('resource.received',function(resource){
        if (resource.contentType.indexOf("json") != -1 && resource.stage == "end") {
            var data = casper.evaluate(function(url){
                // synchronous GET request
                return __utils__.sendAJAX(url, "GET");
            }, resource.url);
            console.log(data)
            // do something with data, you might need to JSON.parse(data)
        }
    })

    reply
    0
  • Cancelreply