我正在尝试使用 PhantomJS 抓取下面提到的 URL 并尝试提取标签中的链接。该链接发回在浏览器上执行的 JavaScript 代码,以在正文中形成 HTML。问题是 PhantomJS 没有执行 Javascript,我在执行代码后得到了未完成的 HTML。
索引.js
var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent';
page.open('http://videohost.site/play/A11QStEaNdVZfvV/', function(status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var html = page.evaluate(function() {
return document.getElementsByTagName('body')[0].innerHTML;
});
console.log(html);
}
phantom.exit();
});
我在控制台中使用 phantomjs index.js 执行它以检查 HTML。任何有关如何执行内部脚本的帮助将不胜感激。
更新:根据 Vaviloff 的建议与 NightMareJS 一起使用。
app.get('/scrape', function (req, res) {
//All the web scraping magic will happen here
console.log('Scrape')
url = 'http://videohost.site/play/A11QStEaNdVZfvV/'
nightmare
.goto(url)
.evaluate(function(){
return document.body.innerHTML; //pass all of the html as text
})
.end()
.then(function (document) {
console.log(document)
})
.catch(function (error) {
console.error('Search failed:', error);
})
res.send('Woohoo')
})
app.listen('8081')
console.log('Magic happens on port 8081');
exports = module.exports = app;