I'm starting with scrapy, and I have first real problem. It's downloading pictures. So this is my spider.
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from example.items import ProductItem
from scrapy.utils.response import get_base_url
import re
class ProductSpider(CrawlSpider):
name = "product"
allowed_domains = ["domain.com"]
start_urls = [
"http://www.domain.com/category/supplies/accessories.do"
]
def parse(self, response):
hxs = HtmlXPathSelector(response)
items = []
sites = hxs.select('//td[@class="thumbtext"]')
number = 0
for site in sites:
item = ProductItem()
xpath = '//div[@class="thumb"]/img/@src'
item['image_urls'] = site.select(xpath).extract()[number]
item['image_urls'] = 'http://www.domain.com' + item['image_urls']
items.append(item)
number = number + 1
return items
When I quote ITEM_PIPELINES
and IMAGES_STORE
in settings.py
this way I get the proper URL for picture I want to download (copy pasted it into browser for check).
But when I unquote those i get following error:
raise ValueError('Missing scheme in request url: %s' % self._url')
exceptions.ValueError: Missing scheme in request url:h
and I can't download my pictures.
I've searched for the whole day and didn't find anything helpful.
Scrapy provides reusable item pipelines for downloading files attached to a particular item (for example, when you scrape products and also want to download their images locally).
Scrapy, being one of the most popular web scraping frameworks, is a great choice if you want to learn how to scrape data from the web. In this tutorial, you'll learn how to get started with Scrapy and you'll also implement an example project to scrape an e-commerce website.
I think the image URL you scraped is relative. To construct the absolute URL use urlparse.urljoin:
def parse(self, response):
...
image_relative_url = hxs.select("...").extract()[0]
import urlparse
image_absolute_url = urlparse.urljoin(response.url, image_relative_url.strip())
item['image_urls'] = [image_absolute_url]
...
Haven't used ITEM_PIPELINES, but the docs say:
In a Spider, you scrape an item and put the URLs of its images into a image_urls field.
So, item['image_urls'] should be a list of image URLs. But your code has:
item['image_urls'] = 'http://www.domain.com' + item['image_urls']
So, i guess it iterates your single URL char by char - using each as URL.
I think that you may need to provide your image url in a list to the Item:
item['image_urls'] = [ 'http://www.domain.com' + item['image_urls'] ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With