简体   繁体   English

如何在Scrapy中正确下载图像?

[英]How to download images without error in Scrapy?

I am newbie to scrapy. 我是新手。 I am trying to download an image. 我正在尝试下载图像。

import scrapy
from scrapy.http import Request


class PlayerSpider(scrapy.Spider):
name = 'player'
#allowed_domains = ['nba.com/players']
start_urls = ['http://www.nba.com/players/']

def parse(self, response):
    Player_Name = response.css('div#content.nba-player-index a ::attr(title)').extract()
    Player_link = response.css('.nba-player-index__trending-item a::attr(href)').extract()
    links  = [url for url in Player_link if url.startswith("/players")]
    for link in links:

        absolute_url = response.urljoin(link)
        yield Request(absolute_url, callback=self.parse_players)

def parse_players(self, response):
    Player_Name = response.css('section.nba-player-header__details-bottom ::text').extract()
    items=[]
    for images in Player_Name:
        item = PlayerSpider()
        images_link = response.css('section.nba-detail-header-wrapper .nba-player-header__headshot img::attr(src)').extract_first()
        image_urls = 'http:{}'.format(images_link)
        item[image_urls]
        return item

    Player_Height = response.css('section.nba-player-vitals__top-left.small-6 p.nba-player-vitals__top-info-imperial ::text').extract()
    Player_Weight = response.css('section.nba-player-vitals__top-right.small-6 p.nba-player-vitals__top-info-imperial ::text').extract()
    yield {
                'Player_name' : Player_Name,
                'Player_Height' : Player_Height,
                'Player Weight' : Player_Weight
        }

I think files are good. 我认为文件很好。 But I am unable to write correct spider for getting the image. 但是我无法编写正确的蜘蛛以获得图像。 I am able to grab the image URL but don't know how to store the image using imagePipeline. 我能够获取图像URL,但是不知道如何使用imagePipeline存储图像。

items.py

import scrapy
from scrapy.item import Item

class PlayerSpider(scrapy.Item):
   image_url = scrapy.Field()
   images = scrapy.Field()
   pass

To enable your image pipeline you must first add it to your project ITEM_PIPELINES setting. 要启用图像管道,必须首先将其添加到项目ITEM_PIPELINES设置中。

settings.py: settings.py:

ITEM_PIPELINES = {'scrapy.pipelines.images.ImagesPipeline': 1}
IMAGES_STORE = 'images'

link 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM