python-scrapy 下载图片

 1 import scrapy 
 2 from PIL import Image
 3 import requests
 4 from io import BytesIO
 5 
 6 class DownloadImages(scrapy.Spider):
 7     name = 'download_images'
 8     start_urls=['https://movie.douban.com/subject/***/reviews']
 9 
10     def parse(self, response):
11         for review in response.css('div.main.review-item'):
12             img_url = review.css('a.avator img::attr(src)').extract_first()
13             img_filename = img_url.split('icon/')[1]
14 
15             res = requests.get(img_url)
16             Image.open(BytesIO(res.content)).save(img_filename)

在python3中,StringIO已经不能使用,只能是用io模块

下载图片到本地-请求图片地址-获取请求响应(unicode)-BytesIO读取-PIL的Image打开-保存在当前文件夹下

猜你喜欢

转载自www.cnblogs.com/manjusaka-min/p/9330359.html