teh*_*abs 6 javascript wget phantomjs
Digikey已经改变了他们的网站,现在有一个通过帖子称为onload的javascript.这杀死了我以前简单的java HTML代码检索器.我想在保存HTML /文本之前使用PhantomJS来允许执行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();
});
}
Run Code Online (Sandbox Code Playgroud)
此代码适用于大多数网页但未通过:
http://search.digikey.com/scripts/dksearch/dksus.dll?keywords=S7072-ND
这是我的测试用例.它无法打开URL,然后PhantomJS崩溃.使用win32 static build 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);
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2490 次 |
| 最近记录: |