目錄
Scrapy 使用ImagePipeline 保存圖片
使用圖片管道?
具體步驟
安裝相關(guān)的依賴庫(kù)
創(chuàng)建Scrapy項(xiàng)目
配置settings.py?
?定義Item
?編寫Spider
運(yùn)行Spider
Scrapy 自定義ImagePipeline
自定義圖片管道
Scrapy 使用ImagePipeline 保存圖片
Scrapy提供了一個(gè) ImagePipeline,用來(lái)下載圖片這條管道,圖片管道ImagesPipeline
?提供了方便并具有額外特性的功能,比如:
- 將所有下載的圖片轉(zhuǎn)換成通用的格式(JPG)和模式(RGB)
- 避免重新下載最近已經(jīng)下載過(guò)的圖片
- 縮略圖生成
- 檢測(cè)圖像的寬/高,確保它們滿足最小限制
使用圖片管道?
scrapy.pipelines.images.ImagesPipeline
使用 ImagesPipeline ,典型的工作流程如下所示:
- 在一個(gè)爬蟲中,把圖片的URL放入?
image_urls
?組內(nèi)(image_urls是個(gè)列表) - URL從爬蟲內(nèi)返回,進(jìn)入圖片管道
- 當(dāng)圖片對(duì)象進(jìn)入 ImagesPipeline,image_urls 組內(nèi)的URLs將被Scrapy的調(diào)度器和下載器安排下載
- settings.py文件中配置保存圖片路徑參數(shù)
IMAGES_STORE
- 開啟管道
需要安裝pillow4.0.0以上版本
pip install pillow==9.2.0
問(wèn)題
報(bào)錯(cuò):twisted.python.failure.Failure OpenSSL.SSL.Error
解決方案
pip uninstall cryptography?pip install cryptography==36.0.2
具體步驟
安裝相關(guān)的依賴庫(kù)
Pillow?
pip install scrapy Pillow
創(chuàng)建Scrapy項(xiàng)目
首先,創(chuàng)建一個(gè)Scrapy項(xiàng)目,可以使用以下命令在命令行中創(chuàng)建一個(gè)新項(xiàng)目:
scrapy startproject myproject
配置settings.py?
打開生成的Scrapy項(xiàng)目文件夾,找到其中的settings.py
文件,編輯該文件,以配置ImagePipeline。
將以下代碼添加到settings.py
文件中:
ITEM_PIPELINES
定義項(xiàng)目中使用的管道,其中scrapy.pipelines.images.ImagesPipeline
是用于處理圖片的管道。1
是優(yōu)先級(jí),數(shù)字越小優(yōu)先級(jí)越高。
IMAGES_STORE
是指定要保存圖片的目錄路徑。將"path/to/save/images"替換為實(shí)際的目錄路徑,以便保存圖片。
ITEM_PIPELINES = {
'scrapy.pipelines.images.ImagesPipeline': 1
}
IMAGES_STORE = 'path/to/save/images'
?定義Item
?在Scrapy項(xiàng)目中,使用Item來(lái)定義要提取的數(shù)據(jù)。在項(xiàng)目的目錄下找到items.py
文件,編輯該文件,以定義一個(gè)Item用于保存圖片鏈接。
定義一個(gè)名為MyItem
的Item,它包含兩個(gè)字段:image_urls
和images
。image_urls
字段用于存儲(chǔ)圖片的URL,images
字段用于存儲(chǔ)已下載的圖片的信息。
import scrapy
class MyItem(scrapy.Item):
image_urls = scrapy.Field()
images = scrapy.Field()
?編寫Spider
在Scrapy項(xiàng)目中,Spider用于定義如何爬取和提取數(shù)據(jù)。在項(xiàng)目的目錄下找到spiders
文件夾,并在該文件夾中創(chuàng)建一個(gè)Python文件,命名為myspider.py
(可以根據(jù)實(shí)際需求命名)。
在myspider.py
文件中,編寫Spider代碼以提取圖片鏈接并將其保存到MyItem
中。
import scrapy
from myproject.items import MyItem
class MySpider(scrapy.Spider):
name = 'myspider'
start_urls = ['http://example.com']
def parse(self, response):
item = MyItem()
item['image_urls'] = response.css('img::attr(src)').getall()
yield item
定義了一個(gè)名為MySpider
的Spider,設(shè)置了起始URL為??????“http://example.com”。在`parse`方法中,我們創(chuàng)建了一個(gè)`MyItem`對(duì)象,使用CSS選擇器提取頁(yè)面中的所有圖片鏈接,并將其賦值給`item['image_urls']`字段。最后,使用`yield`語(yǔ)句將`item`返回。?
運(yùn)行Spider
在命令行中,切換到項(xiàng)目根目錄,并執(zhí)行以下命令運(yùn)行Spider:
scrapy crawl myspider
這將啟動(dòng)Scrapy并開始爬取數(shù)據(jù)。當(dāng)爬取完成時(shí),圖片將自動(dòng)下載并保存到指定的目錄中。
Scrapy 自定義ImagePipeline
問(wèn)題
使用官方默認(rèn)圖片管道,有如下幾個(gè)問(wèn)題:
- 文件名不友好
- 存儲(chǔ)圖片URL的參數(shù)名稱與類型太固定
解決方案
自定義ImagePipeline,擴(kuò)展
自定義圖片管道
-
繼承
scrapy.pipelines.images import ImagesPipeline
-
實(shí)現(xiàn)
get_media_requests(self, item, info)
方法文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-506759.html- 發(fā)送請(qǐng)求,下載圖片
- 轉(zhuǎn)發(fā)文件名
-
實(shí)現(xiàn)
file_path(self,request,response=None,info=None,*,item=None)
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-506759.html- 修改文件名與保存路徑
import re
class Scrapy05Pipeline:
def process_item(self, item, spider):
return item
from scrapy.pipelines.images import ImagesPipeline
from scrapy.http.request import Request
class MyImagePipeline(ImagesPipeline):
def get_media_requests(self, item, info):
return Request(item['image_url'])
def file_path(self, request, response=None, info=None, *, item=None):
# 處理文件名中的特殊字符
# name = item.get('name').strip().replace('\r\n\t\t','').replace('(','').replace(')','').replace('/','_')
name = re.sub('/','_',re.sub('[\s()]','',item.get('name')))
return f'{name}.jpg'
到了這里,關(guān)于python爬蟲之Scrapy框架--保存圖片(詳解)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!