通过 PhantomJs 下载带有 Knockout 绑定的页面
Download page with Knockout bindings via PhantomJs
我想用 PhantomJs 解析 Microsoft Virtual Academy 上的一个页面。例如 this one. I am able to load it (see result) 但在下载的源代码中我没有看到课程描述或持续时间。
要下载页面,我使用了下一种方法:https://gist.github.com/DotNetNerd/5635371。
public string Grab(string url)
{
var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = Config.PhantomJSPath,
Arguments = string.Format("\"{0}\{1}\" {2}", Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName, "index.js", url)
};
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
和 IndexJs
var page = require('webpage').create(),
system = require('system');
page.onLoadFinished = function() {
console.log(page.content);
phantom.exit();
};
page.open(system.args[1]);
我应该将 phantomjs 配置为等待绑定生效,还是 PhantomJs 根本不支持它?
最后,我决定在页面加载完成后等待5s。它不能保证此时一切都会加载,但对我有用。
Index.js 已更新为:
var page = require('webpage').create(),
system = require('system');
page.onLoadFinished = function() {
setTimeout(function () {
console.log(page.content);
phantom.exit();
}, 5000);
};
page.open(system.args[1]);
我想用 PhantomJs 解析 Microsoft Virtual Academy 上的一个页面。例如 this one. I am able to load it (see result) 但在下载的源代码中我没有看到课程描述或持续时间。
要下载页面,我使用了下一种方法:https://gist.github.com/DotNetNerd/5635371。
public string Grab(string url)
{
var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = Config.PhantomJSPath,
Arguments = string.Format("\"{0}\{1}\" {2}", Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName, "index.js", url)
};
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
和 IndexJs
var page = require('webpage').create(),
system = require('system');
page.onLoadFinished = function() {
console.log(page.content);
phantom.exit();
};
page.open(system.args[1]);
我应该将 phantomjs 配置为等待绑定生效,还是 PhantomJs 根本不支持它?
最后,我决定在页面加载完成后等待5s。它不能保证此时一切都会加载,但对我有用。
Index.js 已更新为:
var page = require('webpage').create(),
system = require('system');
page.onLoadFinished = function() {
setTimeout(function () {
console.log(page.content);
phantom.exit();
}, 5000);
};
page.open(system.args[1]);