javascript 如何使用 PhantomJS 获取网站的 HTML 源代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20173928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:56:29 来源:igfitidea点击:
How to get the HTML source of a website with PhantomJS
提问by MOB
Below is an example of PhantomJS that gets some element by DOM id from an external webpage:
下面是 PhantomJS 的一个例子,它通过 DOM id 从外部网页获取一些元素:
var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent';
page.open('http://www.httpuseragent.org', function(status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var ua = page.evaluate(function() {
return document.getElementById('myagent').textContent;
});
console.log(ua);
}
phantom.exit();
});
I want to get the entire HTML source of a webpage ... how do I do this?
我想获取网页的整个 HTML 源代码……我该怎么做?
回答by Hessam
All you have to do is to use page.content
您所要做的就是使用 page.content
var page = require('webpage').create();
page.onError = function(msg, trace) {
//prevent js errors from showing in page.content
return;
};
page.open('http://www.httpuseragent.org', function () {
console.log(page.content); //page source
phantom.exit();
});