gpt4 book ai didi

javascript - 使用 PhantomJS 获取由 Javascript 呈现的 html

转载 作者:可可西里 更新时间:2023-11-01 14:48:03 25 4
gpt4 key购买 nike

我正在尝试利用 PhantomJS 获取动态页面生成的 html。我原以为这很容易,但经过几个小时的尝试,我仍然不走运。

页面本身有这个源代码以及最终保存在 1.html 中的内容:

<!doctype html>
<html lang="cs" ng-app="appId">
<head ng-controller="MainCtrl">
(ommited some lines)
<script src="/js/conf/config.js?pars"></script>
<script src="/js/all.js?pars"></script>
</head>
<body>
<!--<![endif]-->
<div site-loader></div>
<div page-layout>
<div ng-view></div>
</div>
</body>
</html>

web 的所有内容都加载到 site-loader div 中,但我没有运气得到它,即使我在通过 PhantomJS 抓取 html 之前使用超时。这是我正在使用的代码:

var url = 'http:...';
var page = require('webpage').create();
var fs = require('fs');

page.open(url, function (status) {
if (status !== 'success') {
console.log('Fail');
phantom.exit();
} else {
window.setTimeout(function () {
fs.write('1.html', page.content, 'w');
phantom.exit();
}, 2000); // Change timeout as required to allow sufficient time
}
});

请问我做错了什么?

编辑:我决定尝试 PJscrapper 框架并将其配置为抓取 div block 的所有内容。我得到的一切都很糟糕:

["","\n\t\tif (window.DOT) {\n\t\t\tDOT.cfg({service: 'sreality', impress: false});\n\t\t}\n\t","","Loader.load()","",""]

似乎我真的不明白它并且总是在 Loader.load() Action 之前获取代码。显然,超时并不能解决问题。

最佳答案

这样就可以了

    page.open(url, function (status) {
if (status !== 'success') {
console.log('Unable to load the url!');
phantom.exit();
} else {
window.setTimeout(function () {
var results = page.evaluate(function() {
return document.documentElement.innerHTML;
});
console.log(results)
phantom.exit();
}, 200);
}
});

关于javascript - 使用 PhantomJS 获取由 Javascript 呈现的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25740213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com