使用PhantomJS获取JSON页面内容 [英] Get JSON page content with PhantomJS

查看:124
本文介绍了使用PhantomJS获取JSON页面内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在phantomjs中解析JSON.任何页面内容都包含在html(<html><body><pre>{JSON string}</pre></body></html>)中.是否可以删除封闭标签或要求将其他Content-Type用作"application/json"?如果不是,解析它的最佳方法是什么.包括includeJS jQuery之后是否使用jQuery?

I would like to know how to parse JSON in phantomjs. Any page content is enclosed in html (<html><body><pre>{JSON string}</pre></body></html>). Is there an options to remove enclosing tags or asking for a different Content-Type as "application/json"? If not, what's the best way to parse it. Is it using jQuery after including with includeJS jQuery?

推荐答案

由于您使用的是由Webkit浏览器构建的PhantomJS,因此可以访问本机JSON库.无需使用page.evaluate,您只需在页面对象上使用plainText属性即可.

Since you are using PhantomJS which is built of the webkit browser you have access to the native JSON library. There is no need to use page.evaluate, you can just use the plainText property on the page object.

http://phantomjs.org/api/webpage/property/plain-text.html

var page = require('webpage').create();
page.open('http://somejsonpage.com', function () {
    var jsonSource = page.plainText;
    var resultObject = JSON.parse(jsonSource);
    phantom.exit();
});

这篇关于使用PhantomJS获取JSON页面内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆