如何使用phantom.js抓取JavaScript注入的图像src和alt? [英] How to scrape javascript injected image src and alt with phantom.js?
问题描述
我正在使用以下脚本使用phantom.js刮取图像:
I'm using the following script to scrape images using phantom.js:
var page = require('webpage').create();
url = 'https://www.everlane.com/collections/mens-luxury-tees/products/mens-crew-antique'
page.open(url, function(status) {
if (status !== 'success') {
console.log('error');
phantom.exit();
return;
}
var a = page.evaluate(function() {
return document.getElementsByTagName('img');
});
SrcAlt = [];
for (var i=0; i<a.length; i++){
var src = a[i].getAttribute('src');
var alt = a[i].getAttribute('alt');
SrcAlt.push({"src": src, "alt": alt});
}
console.log(SrcAlt);
phantom.exit();
});
但是,脚本在我定义变量a
之后挂起,这意味着它不会迭代并返回src和alt属性.当我console.log(a)
时它返回34,所以我相信我拥有适当的图像资源.如何访问src和alt信息?谢谢!
But, the script hangs after I define the variable a
, meaning it doesn't iterate through and return the src and alt attributes. When I console.log(a)
it returns 34, so I believe I have the proper image resources. How can I access the src and alt information? Thanks!
推荐答案
页面加载完成后,您需要评估您的页面.您可以通过使用page.onLoadFinished
回调来做到这一点.所有页面内容加载完毕并准备好文档后,将调用此回调.这样的事情应该起作用:
You need to evaluate your page after the page has finished loading. You can do this by using the page.onLoadFinished
callback. This callback is called after all page content is finished loading and the document is ready. Something like this should work:
var page = require('webpage').create();
var url = 'https://www.everlane.com/collections/mens-luxury-tees/products/mens-crew-antique';
page.open(url);
page.onLoadFinished = function()
{
var a = page.evaluate(function() {
return document.getElementsByTagName('img');
});
SrcAlt = [];
for (var i=0; i<a.length; i++){
var src = a[i].getAttribute('src');
var alt = a[i].getAttribute('alt');
SrcAlt.push({"src": src, "alt": alt});
}
console.log(SrcAlt);
phantom.exit();
}
这篇关于如何使用phantom.js抓取JavaScript注入的图像src和alt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!