如何用r语言爬取json的数据

Python010

如何用r语言爬取json的数据,第1张

如果以后抓取网页碰到动态加载的数据,可以考虑使用 phantomjs 如果想更暴力直接开出一个有界面的浏览器做各式各样的操作,达到ajax无阻碍的,可以用Selenium + Beautifulsoup

服务端 Look here

我用.NET新建一个解决方案,里面包含了2个项目,一个web项目,另外一个是winform项目,当然你也可以弄2个web项目,不影响部署。

<1>服务端phantomjs搭建

一些准备文件

phantomjs.exe 和 highcharts工具包

winform界面写出来

winform后端核心代码

#region 启动进程

Process p = new Process()

p.StartInfo.FileName = Environment.CurrentDirectory + "//phantomjs//phantomjs_1.9V.exe"

string ExcuteArg = Environment.CurrentDirectory + "//script//highcharts-convert.js -host 127.0.0.1 -port 3003"

p.StartInfo.Arguments = string.Format(ExcuteArg)

p.StartInfo.CreateNoWindow = false

p.StartInfo.UseShellExecute = false

//重定向标准输出

p.StartInfo.RedirectStandardOutput = true

//重定向错误输出

p.StartInfo.RedirectStandardError = false

p.StartInfo.WindowStyle = ProcessWindowStyle.Normal

string[] result = { }

if (!p.Start())

{

throw new Exception("无法启动Headless测试引擎.")

}

result = p.StandardOutput.ReadToEnd().Split(new char[] { '\r', '\n' })

if (result.Length == 0)

{

result[0] = "已成功启动,但无数据"

}

foreach (string s in result)

{

list_Msg.Items.Add(s)

}

#endregion

<2>web端搭建及如何调用phantomjs

web页面搭建

实际上这个步骤可以省略,只是为了展示返回的数据而已,毕竟可以纯后端生成。