# 图片最小高度和宽度设置,可以过滤太小的图片
IMAGES_MIN_HEIGHT = 110
IMAGES_MIN_WIDTH = 110
# 生成缩略图选项
IMAGES_THUMBS = {
'small': (50, 50),
'big': (270, 270),
}
class PhotoGalleryPipeline(object):
...
class PhotoPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
for (id, url) in item['images'].items():
yield scrapy.Request(url)
ITEM_PIPELINES = {
'Toutiao.pipelines.PhotoGalleryPipeline': 300,
'Toutiao.pipelines.PhotoPipeline': 200,
}
...
def file_path(self, request, response=None, info=None):
file_name = request.url.split('/')[-1]
return 'full/%s' % (file_name)
...
def item_completed(self, results, item, info)
[(True,
{'checksum': '2b00042f7481c7b056c4b410d28f33cf',
'path': 'full/0a79c461a4062ac383dc4fade7bc09f1384a3910.jpg',
'url': 'http://www.example.com/files/product1.pdf'}),
(False,
Failure(...))]
def item_completed(self, results, item, info):
image_paths = {x['url'].split('/')[-1]: x['path'] for ok, x in results if ok}
if not image_paths:
# 下载失败忽略该 Item 的后续处理
raise DropItem("Item contains no files")
else:
# 将图片转移至以 post_id 为名的子目录中
for (dest, src) in image_paths.items():
dir = settings.IMAGES_STORE
newdir = dir + os.path.dirname(src) + '/' + item['post_id'] + '/'
if not os.path.exists(newdir):
os.makedirs(newdir)
os.rename(dir + src, newdir + dest)
# 将保存路径保存于 item 中(image_paths 需要在 items.py 中定义)
item['image_paths'] = image_paths
return item
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有