我正在尝试使用phantomjs从网站http://www.lenskart.com/eyeglasses/frame-style/half-rim.html抓取一些产品信息。以下是我使用的脚本:
var page = require('webpage').create();
page.onConsoleMessage = function(msg){
console.log(msg);
};
page.open('http://www.lenskart.com/eyeglasses/frame-style/half-rim.html', function () {
console.log('1234');
// Checks for bottom div and scrolls down from time to time
window.setInterval(function() {
console.log('ddddddddddd');
// Checks if there is a div with class=".has-more-items"
// (not sure if this is the best way of doing it)
var count = page.evaluate(function() {
try{
return document.getElementsByClassName('top-to-bottom')[0].style.display;
}catch(e){return e.message;}
});
//count = 1
console.log(count);
console.log('1111 '+count);
if(count === 'block' || count ==='inline' ) { // Didn't find
console.log('count '+count);
//this.echo('wdc');
page.evaluate(function() {
// Scrolls to the bottom of page
window.document.body.scrollTop = document.body.scrollHeight;
});
}
else
{ // Found
//Do what you want
console.log('len123');
try {
var links = page.evaluate(function() {
return [].map.call(document.querySelectorAll('div.product-view-region a.product-image'), function(link) {
//console.log(link.getAttribute('href'));
return link.getAttribute('href');
});
});
} catch (e) {
console.log(e.message); return [];
}
//console.log(links.join(','));
//console.log(links);
phantom.exit();
}
}, 1000); // Number o ms to wait between scrolls
});
但我不断得到这个错误:
TypeError: 'undefined' is not a function (evaluating 'this.each(function(value,i
ndex){results.push(iterator.call(context,value,index));})')
http://d37l6i9uhpr090.cloudfront.net/js/top_common.js:809 in collect
phantomjs://webpage.evaluate():6
phantomjs://webpage.evaluate():7
phantomjs://webpage.evaluate():7
null
我还试图抓取其他一些信息,但得到了相同的结果。我不明白我哪里错了!