Scrapy is not able to download the images from a URL

541 Views Asked by At

I am using scrapy to download the images but it is not working. I get the URL in desired folder but not the images.

Here is my items.py:

class Brand(scrapy.Item):
    name = scrapy.Field()
    url = scrapy.Field()
    brand_image = scrapy.Field()
    image_urls = scrapy.Field()
    images = scrapy.Field()
    pass

Here is my spider code:

import scrapy
import json
from scraper.items import Brand


class QuotesSpider(scrapy.Spider):
    name = "brandDetails"

    def start_requests(self):
        with open('brands.json') as data_file:
            data_item = json.load(data_file)
        urls = list()
        for item in data_item:
            urls.append(item["url"])
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        item = Brand()
        name = response.css("div.th::text").extract_first()
        name = name.replace('Products of ', '')
        item['name'] = name
        item['url'] = response.url
        urls = response.css("div.productimage img::attr(src)").extract_first()
        urls = "http://ozhat-turkiye.com" + urls
        item['image_urls'] = urls
        yield item

Here is the setting code:

BOT_NAME = 'scraper'
SPIDER_MODULES = ['scraper.spiders']
NEWSPIDER_MODULE = 'scraper.spiders'
IMAGE_STORE = 'C:/Users/SHAHRUKH/Desktop/AI'
ITEM_PIPELINES = {
  'scrapy.pipelines.images.ImagesPipeline': 1
}
DOWNLOAD_DELAY = 2

Here is the output of my program:

 {'image_urls': 'http://ozhat-turkiye.com/get.aspx?id=1882267',
 'name': ' Camille Bauer',
 'url': 'http://ozhat-turkiye.com/en/camille-bauer/'}
1

There are 1 best solutions below

8
On

You can down those images and store it using urllib.

import urllib     

urllib.urlretrieve(img_url, filename)

For example code check here.