练习(四)
目标抓取
- 抓取用户头像图片
图像处理支持
官方推荐使用Pillow替代PIL
#安装pillow
pip install Pillow
新建一个spider用于爬取用户头像
scrapy genspider head segmentfault.com
修改保存图像配置
ITEM_PIPELINES = {
'scrapy.pipelines.images.ImagesPipeline': 300 #图片处理Pipeline
}
#图片保存位置
IMAGES_STORE = 'E:\\coding\\segmentfault\\head image'
#下面是可选设置
# 30天图像过期时间
IMAGES_EXPIRES = 30
#缩率图设置
IMAGES_THUMBS = {
'small': (50, 50),
'big': (270, 270),
}
#过滤图片最小宽(高)度
IMAGES_MIN_HEIGHT = 110
IMAGES_MIN_WIDTH = 110
修改parse方法
def parse(self, response):
images = response.css('img.program-avatar64') #提取所有图片元素
for img in images:
#image_urls属性为默认属性用于收集图片地址集合,可以是item的字段也可以是dict的关键字
yield {"image_urls" : img.css("::attr(src)").extract()}
执行后我们可以在我们配置的图像保存目录(E:\coding\segmentfault\head image)中看到爬取的图片