Digikey 更改了他们的网站,现在有一个名为 onload via post 的 javascript。这杀死了我以前的简单 java HTML 代码检索器。我正在尝试使用 PhantomJS 来允许在保存 HTML/文本之前执行 javascript。
var page = new WebPage(),
t, address;
var fs = require('fs');
if (phantom.args.length === 0) {
console.log('Usage: save.js <some URL>');
phantom.exit();
} else {
address = encodeURI(phantom.args[0]);
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
f = null;
var markup = page.content;
console.log(markup);
try {
f = fs.open('htmlcode.txt', "w");
f.write(markup);
f.close();
} catch (e) {
console.log(e);
}
}
phantom.exit();
});
}
此代码适用于大多数网页,但在以下情况下失败:
http://search.digikey.com/scripts/dksearch/dksus.dll?keywords=S7072-ND http://search.digikey.com/scripts/dksearch/dksus.dll?keywords=S7072-ND
这是我的测试用例。它无法打开 URL,然后 PhantomJS 崩溃。使用win32静态构建1.3。
有小费吗?
基本上我所追求的是 wget 来竞争页面渲染和保存文件之前修改文档的脚本。
一个快速但肮脏的解决方案...但已发布在 phantomjs 网站上...是使用超时。我已经修改了您的代码以包含 2 秒的等待。这允许页面在将内容转储到文件之前加载 2 秒。如果您需要精确的秒数或时间量会有很大差异,则此解决方案可能不适合您。
var page = new WebPage(),
t, address;
var fs = require('fs');
if (phantom.args.length === 0) {
console.log('Usage: save.js <some URL>');
phantom.exit();
} else {
address = encodeURI(phantom.args[0]);
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
window.setTimeout(function(){
f = null;
var markup = page.content;
console.log(markup);
try {
f = fs.open('htmlcode.txt', "w");
f.write(markup);
f.close();
} catch (e) {
console.log(e);
}
}
phantom.exit();
},2000);
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)