Javascript 使用phantom.js刮取数据
在此基础上,我尝试使用phantomjs刮取数据,修改以下脚本: 我的目标是将一个工作函数(参见第2个代码段)集成到下面第1个代码段中的脚本中。我尝试过这样做,但不断出错。有没有一种方法可以真正实现集成 (注意:使用phantomjs是因为该网站是一个angular应用程序,其中初始HTML不包含我要查找的任何数据,即无头web浏览器。因此我需要将页面加载到内存中,等待angular完成任务(某种延迟设置),然后刮取呈现的DOM) 当我执行脚本(phantomjs scraping.js)时,我得到的错误(和输出)如下: 控制台>SPR-错误:103-发布日期无效控制台>v6 ReferenceError:找不到变量:angularJavascript 使用phantom.js刮取数据,javascript,jquery,html,node.js,phantomjs,Javascript,Jquery,Html,Node.js,Phantomjs,在此基础上,我尝试使用phantomjs刮取数据,修改以下脚本: 我的目标是将一个工作函数(参见第2个代码段)集成到下面第1个代码段中的脚本中。我尝试过这样做,但不断出错。有没有一种方法可以真正实现集成 (注意:使用phantomjs是因为该网站是一个angular应用程序,其中初始HTML不包含我要查找的任何数据,即无头web浏览器。因此我需要将页面加载到内存中,等待angular完成任务(某种延迟设置),然后刮取呈现的DOM) 当我执行脚本(phantomjs scraping.js)时,我
控制台>SPR-ERROR:103-发布日期无效(日期)
==================================================== 步骤“0” ======================================================================
控制台>已访问数据 控制台> 似乎它正在连接到所需的站点。如何修改下面的脚本以适应此qn底部的提取代码:
var page = new WebPage(),
url = 'http://www.inc.com/inc5000/index.html',
stepIndex = 0;
/**
* From PhantomJS documentation:
* This callback is invoked when there is a JavaScript console. The callback may accept up to three arguments:
* the string for the message, the line number, and the source identifier.
*/
page.onConsoleMessage = function (msg, line, source) {
console.log('console> ' + msg);
};
/**
* From PhantomJS documentation:
* This callback is invoked when there is a JavaScript alert. The only argument passed to the callback is the string for the message.
*/
page.onAlert = function (msg) {
console.log('alert!!> ' + msg);
};
// Callback is executed each time a page is loaded...
page.open(url, function (status) {
if (status === 'success') {
// State is initially empty. State is persisted between page loads and can be used for identifying which page we're on.
console.log('============================================');
console.log('Step "' + stepIndex + '"');
console.log('============================================');
// Inject jQuery for scraping (you need to save jquery-1.6.1.min.js in the same folder as this file)
page.injectJs('jquery-1.6.1.min.js');
// Our "event loop"
if(!phantom.state){
//initialize();
scrapeData();
} else {
phantom.state();
}
// Save screenshot for debugging purposes
page.render("step" + stepIndex++ + ".png");
}
});
function scrapeData(){
page.evaluate(function() {
console.log('Reached scrapeData');
var DATA = [];
$('tr.ng-scope').each(function(){
var $tds = $(this).find('td');
DATA.push({
rank: $tds.eq(0).text(),
company: $tds.eq(1).text(),
growth: $tds.eq(2).text(),
revenue: $tds.eq(3).text(),
industry: $tds.eq(4).text()
});
});
console.log(DATA);
});
phantom.state = parseResults;
// scraping code here
}
// Step 1
function initialize() {
page.evaluate(function() {
console.log('Searching...');
});
// Phantom state doesn't change between page reloads
// We use the state to store the search result handler, ie. the next step
phantom.state = parseResults;
}
// Step 2
function parseResults() {
page.evaluate(function() {
$('#search-result a').each(function(index, link) {
console.log($(link).attr('href'));
})
console.log('Parsed results');
});
// If there was a 3rd step we could point to another function
// but we would have to reload the page for the callback to be called again
phantom.exit();
}
我知道下面的代码在控制台中工作,但我如何将其与上面的代码脚本集成,以成功地从站点的多个页面中获取数据:
request('http://www.inc.com/inc5000/index.html', function (error, response, html) {
if(error || response.statusCode != 200) return;
var $ = cheerio.load(html);
var DATA = [];
$('tr.ng-scope').each(function(){
var $tds = $(this).find('td');
DATA.push({
rank: $tds.eq(0).text(),
company: $tds.eq(1).text(),
growth: $tds.eq(2).text(),
revenue: $tds.eq(3).text(),
industry: $tds.eq(4).text()
});
});
console.log(DATA);
});
如果你注意到的话,这篇文章可能会重复,我也是写这篇文章的人。我写了一个不同的脚本,因此我要问一个单独的问题,因为这个特定的脚本不工作。我希望我们能找到解决方案,而不是长时间讨论是否发生了重复。当您删除不必要的函数(
initialize
,parseResults
)和“事件循环”时,您就可以找到链接问题中的代码。目标似乎也一样。如果你澄清目标不同,那么我可以撤回我的投票。