汽車(chē)之家圖片下載(爬蟲(chóng)代碼)

bmw/spiders/bmw5.py

from scrapy.spidersimport CrawlSpider,Rule

from scrapy.linkextractorsimport LinkExtractor

from bmw.itemsimport BmwItem

class Bmw5Spider(CrawlSpider):

? ? name= 'bmw5'

? ? allowed_domains= ['car.autohome.com.cn']

????start_urls= ['https://car.autohome.com.cn/pic/series/159.html']

# https://car.autohome.com.cn/pic/series/159-10.html#pvareaid=2042222

# https://car.autohome.com.cn/pic/series/159-51-p2.html

? ? rules= (

????Rule(LinkExtractor(allow="https://car.autohome.com.cn/pic/series/159.+"),callback='parse_page',follow=True),

)

def parse_page(self,response):

? ? ? ? title= response.xpath("http://div[@class='uibox']/div/text()").get()

????????srcs= response.xpath("http://div[contains(@class,'uibox-con')]/ul/li//img/@src").getall()

????????urls= list(map(lambda x:response.urljoin(x.replace('240x180_0_q95_c42',"1024x0_1_q95")),srcs))

????????yield BmwItem(title=title,image_urls=urls)

bmw/items.py

import scrapy

class BmwItem(scrapy.Item):

? ? title= scrapy.Field()

????image_urls= scrapy.Field()

????images= scrapy.Field()

bmw/pipelines.py

import os

from urllibimport request

from scrapy.pipelines.imagesimport ImagesPipeline

from bmwimport settings

class BmwPipelines(ImagesPipeline):

? ? def get_media_requests(self,item,info):

? ? ? ? request_objs= super(BmwPipelines,self).get_media_requests(item,info)

????????for request_objin request_objs:

? ? ? ? ? ? request_obj.item= item

? ? ? ? return request_objs

def file_path(self,request,response=None,info=None):

? ? ? ? path= super(BmwPipelines,self).file_path(request,response,info)

????????title= request.item.get('title')

????????images_store= settings.IMAGES_STORE

????????title_path= os.path.join(images_store,title)

????????if not os.path.exists(title_path):

? ? ? ? ? ? os.mkdir(title_path)

????????image_name= path.replace("full/","")

????????image_path= os.path.join(title_path,image_name)

????????return image_path

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 目的 Scrapy框架為文件和圖片的下載專門(mén)提供了兩個(gè)Item Pipeline 它們分別是: FilePipel...
    緣來(lái)閱讀 1,997評(píng)論 0 0
  • 說(shuō)起寫(xiě)爬蟲(chóng),大多數(shù)第一時(shí)間想到的就是python了。python語(yǔ)法簡(jiǎn)潔明了,加上及其豐富好用的庫(kù),用它來(lái)寫(xiě)爬蟲(chóng)有...
    瘋狂的哈丘閱讀 8,364評(píng)論 1 15
  • 1 安裝Scrapy Scrapy是一個(gè)為了爬取網(wǎng)站數(shù)據(jù),提取結(jié)構(gòu)性數(shù)據(jù)而編寫(xiě)的應(yīng)用框架。 可以應(yīng)用在包括數(shù)據(jù)挖掘...
    superzhan閱讀 1,558評(píng)論 0 11
  • 這是偶爾在網(wǎng)上看到的一幅畫(huà),我把它保存下來(lái),用彩鉛涂鴉,在此記錄一下我的過(guò)程。 下面是線稿哦 再用針管筆描一邊,用...
    月微寒閱讀 1,206評(píng)論 2 16
  • 親愛(ài)的麗琴昨天你好棒,你沒(méi)有用過(guò)激的行為和情緒去面對(duì)問(wèn)題,你也很平穩(wěn)的處理好了與丈夫,孩子之間的問(wèn)題。而且還支持丈...
    張利勤閱讀 323評(píng)論 0 4

友情鏈接更多精彩內(nèi)容