【发布时间】:2013-12-09 01:10:41
【问题描述】:
下面是一个 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();
});
我想获取网页的整个 HTML 源代码……我该怎么做?
【问题讨论】:
-
如果你想要 HTML 源代码,那么使用类似 the http module 的东西,而不是通过浏览器运行页面(它将执行 JS 并用它来破坏 DOM)。
标签: javascript phantomjs