距离上一个答案已经过去几年了,有些事情发生了变化(2015 年)。 Nick Verwymeren 写了一篇博客文章,详细介绍了如何执行此操作的更新版本。他的博文在这里:https://www.nickv.codes/blog/scrapy-uploading-image-files-to-amazon-s3/
在您的 settings.py 文件中:
ITEM_PIPELINES = {
'scrapy.contrib.pipeline.images.ImagesPipeline': 1
}
# This is going to be the amazon s3 bucket.
# You need to use the below format so Scrapy
# can parse it. !!Important don't forget to add
# the trailing slash.
IMAGES_STORE = 's3://my-bucket-name/'
# The amount of days until we re-download the image
IMAGES_EXPIRES = 180
# You can add as many of these as you want
IMAGES_THUMBS = {
'small': (50, 50),
'big': (300, 300)
}
AWS_ACCESS_KEY_ID = 'your-access-key'
AWS_SECRET_ACCESS_KEY= 'your-secret-access-key'
为了安全起见,我建议在 Amazon AWS 界面中创建一个新用户,并授予该用户对您的存储桶的只读/写入权限。
现在我们需要安装一些 Scrapy 没有默认提供的包:
pip install pillow
pip intall botocore
Pillow 处理图像操作,boto 将提供连接到 S3 的库。
Scrapy 使用您项目中的 image_urls 键来查找它应该下载的图像。这应该是图像 url 的列表。下载后,Scrapy 会将图像位置的详细信息写入 images 键。
不要忘记将这些添加到您的 items.py 文件中:
class MyItem(scrapy.Item):
image_urls = scrapy.Field()
images = scrapy.Field()
现在不要忘记在抓取过程中实际填充 image_urls 键。爬取网站后,给定项目的最终输出将如下所示:
'image_urls': [u'http://example.com/images/tshirt.jpg'],
'images': [{ 'checksum': '264d3bbdffd4ab3dcb8f234c51329da8',
'path': 'full/069f409fd4cdb02248d726a625fecd8299e6055e.jpg',
'url': 'http://example.com/images/tshirt.jpg'}],
现在前往您的亚马逊 S3 存储桶并看看。你的图片和缩略图都在那里!
再次感谢 Nick Verwymeren 的博文准确回答了这个问题!