如何使用 python Scrapy 抓取延迟加载图像
How to scrape lazy loading images using python Scrapy
这是我用来抓取网页的代码。我要抓取的站点启用了图片延迟加载,所以scrapy只能抓取100张图片中的10张,其余都是placeholder.jpg。在 Scrapy 中处理延迟加载图像的最佳方法是什么?
谢谢!
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)
CasperJS 等其他工具似乎有加载图像的视口。
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);
});
问题是延迟加载是由 Javascript 进行的,scrapy 无法处理,casperjs 可以处理这个问题。
要使此功能与 scrapy 一起使用,您必须将其与 Selenium 或 scrapyjs 混合使用
要在延迟加载中抓取图像,您必须跟踪 ajax 请求哪些 returns 个图像。在此之后,您在 scrapy 中点击了该请求。从某个页面获取所有数据后。您必须在 scrapy 请求中通过元数据将提取的数据发送到其他回调。如需进一步帮助 Scrapy request
这是我用来抓取网页的代码。我要抓取的站点启用了图片延迟加载,所以scrapy只能抓取100张图片中的10张,其余都是placeholder.jpg。在 Scrapy 中处理延迟加载图像的最佳方法是什么?
谢谢!
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)
CasperJS 等其他工具似乎有加载图像的视口。
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);
});
问题是延迟加载是由 Javascript 进行的,scrapy 无法处理,casperjs 可以处理这个问题。
要使此功能与 scrapy 一起使用,您必须将其与 Selenium 或 scrapyjs 混合使用
要在延迟加载中抓取图像,您必须跟踪 ajax 请求哪些 returns 个图像。在此之后,您在 scrapy 中点击了该请求。从某个页面获取所有数据后。您必须在 scrapy 请求中通过元数据将提取的数据发送到其他回调。如需进一步帮助 Scrapy request