Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scrape lazy loading images using python Scrapy

Here is the code which i used for crawling a web page. The site I want to scrape has images lazy loading enabled, so scrapy can only grab 10 out of 100 images, the rest are all placeholder.jpg. What would be the best way to deal with lazy loading images in Scrapy?

Thanks!

class MasseffectSpider(scrapy.Spider):
name = "massEffect"
allowed_domains = ["amazon.com"]
start_urls = [
    'file://127.0.0.1/home/ec2-user/scrapy/amazon/amazon.html',
]


def parse(self, response):

for item in items:
    listing = Item()
    listing['image'] =  item.css('div.product img::attr(src)').extract()
    listing['url'] =  item.css('div.item-name a::attr(href)').extract()
    listings.append(listing)

It seems other tools like CasperJS has the viewport to load the images.

casper.start('http://m.facebook.com', function() {

// The pretty HUGE viewport allows for roughly 1200 images.
// If you need more you can either resize the viewport or scroll down the viewport to load more DOM (probably the best approach).
this.viewport(2048,4096);

this.fill('form#login_form', {
    'email': login_username,
    'pass':  login_password
}, true);
});
like image 445
Will W Avatar asked Sep 03 '26 12:09

Will W


1 Answers

The problem is that lazy loading is being made by Javascript which scrapy can't handle, casperjs handles this.

To make this work with scrapy you have to mix it with Selenium or scrapyjs

like image 184
Rafael Almeida Avatar answered Sep 06 '26 03:09

Rafael Almeida



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!