【问题标题】:Trouble with downloading images using Scrapy使用 Scrapy 下载图像时遇到问题
【发布时间】:2015-04-28 14:57:32
【问题描述】:

我在尝试使用带有 Scrapy 的蜘蛛下载图像时遇到以下错误。

File "C:\Python27\lib\site-packages\scrapy\http\request\__init__.py",
line 61, in _set_url
            raise ValueError('Missing scheme in request url: %s' % self._url)
        exceptions.ValueError: Missing scheme in request url: h

尽我所能理解,似乎我在某个地方的网址中缺少一个“h”?但我一辈子都看不到在哪里。如果我不尝试下载图像,一切正常。但是一旦我将适当的代码添加到下面的四个文件中,我就无法正常工作。谁能帮我理解这个错误?

items.py

import scrapy

class ProductItem(scrapy.Item):
    model = scrapy.Field()
    shortdesc = scrapy.Field()
    desc = scrapy.Field()
    series = scrapy.Field()
    imageorig = scrapy.Field()
    image_urls = scrapy.Field()
    images = scrapy.Field()

settings.py

BOT_NAME = 'allenheath'

SPIDER_MODULES = ['allenheath.spiders']
NEWSPIDER_MODULE = 'allenheath.spiders'

ITEM_PIPELINES = {'scrapy.contrib.pipeline.images.ImagesPipeline': 1}

IMAGES_STORE = 'c:/allenheath/images'

pipelines.py

class AllenheathPipeline(object):
    def process_item(self, item, spider):
        return item

import scrapy
from scrapy.contrib.pipeline.images import ImagesPipeline
from scrapy.exceptions import DropItem

class MyImagesPipeline(ImagesPipeline):

    def get_media_requests(self, item, info):
        for image_url in item['image_urls']:
            yield scrapy.Request(image_url)

    def item_completed(self, results, item, info):
        image_paths = [x['path'] for ok, x in results if ok]
        if not image_paths:
            raise DropItem("Item contains no images")
        item['image_paths'] = image_paths
        return item

products.py(我的蜘蛛)

import scrapy

from allenheath.items import ProductItem
from scrapy.selector import Selector
from scrapy.http import HtmlResponse

class productsSpider(scrapy.Spider):
    name = "products"
    allowed_domains = ["http://www.allen-heath.com/"]
    start_urls = [
        "http://www.allen-heath.com/ahproducts/ilive-80/",
        "http://www.allen-heath.com/ahproducts/ilive-112/"
    ]

    def parse(self, response):
        for sel in response.xpath('/html'):
            item = ProductItem()
            item['model'] = sel.css('#prodsingleouter > div > div > h2::text').extract()
            item['shortdesc'] = sel.css('#prodsingleouter > div > div > h3::text').extract()
            item['desc'] = sel.css('#tab1 #productcontent').extract()
            item['series'] = sel.css('#pagestrip > div > div > a:nth-child(3)::text').extract()
            item['imageorig'] = sel.css('#prodsingleouter > div > div > h2::text').extract()
            item['image_urls'] = sel.css('#tab1 #productcontent img').extract()[0]
            item['image_urls'] = 'http://www.allen-heath.com' + item['image_urls']
            yield item

任何帮助将不胜感激。

【问题讨论】:

    标签: python scrapy


    【解决方案1】:

    问题就在这里:

    def get_media_requests(self, item, info):
        for image_url in item['image_urls']:
            yield scrapy.Request(image_url)
    

    这里:

    item['image_urls'] = sel.css('#tab1 #productcontent img').extract()[0]
    

    您正在提取此字段并获取第一个元素。这意味着,一旦您在管道中对其进行迭代,您实际上就是在对以 http 开头的 URL 中的字符进行迭代 - 只要尝试处理第一个字母,就会解释您看到的错误消息:

    Missing scheme in request url: h
    

    从该行中删除 [0]。当您使用它时,获取图像的src,而不是整个元素:

    item['image_urls'] = sel.css('#tab1 #productcontent img').xpath('./@src').extract()
    

    之后,您还应该更新下一行,以防图像 url 是相对的,以将其转换为绝对:

    import urlparse  # put this at the top of the script
    item['image_urls'] = [urlparse.urljoin(response.url, url) for url in item['image_urls']]
    

    但如果src 中的图像 URL 实际上是绝对的,则不需要最后一部分,因此只需将其删除。

    【讨论】:

    • 删除 [0] 消除了该错误。但是又出现了一个新的错误。紧随其后的行将域字符串与图像 url 结合起来。我这样做是为了防止图像使用相对路径。 (虽然我认为这个站点没有必要)错误是:File "C:\allenheath\allenheath\spiders\products.py", line 24, in parse item['image_urls'] = 'http://www.allen-heath.com' + item['image_urls'] exceptions.TypeError: cannot concatenate 'str' and 'list' objects 如果我完全删除它,我的脚本运行没有错误。但是我仍然没有得到任何图像。
    • 谢谢。这解决了该行的问题。我可以把它放在那里,它不再出错了。但是,当我运行它时,我仍然没有下载任何图像。我将结果导出到 csv,它正在写一个 image_urls 列,所以我知道它可以看到我想要抓取的图像。它只是不下载它们。我的代码的另一部分肯定遗漏了其他内容。
    • 这是我得到的一个错误,它可能会有所帮助。 2015-04-28 10:38:23-0700 [products] DEBUG: Retrying <GET http://www.allen-heath.com%3Cimg%20class=%22alignnone%20size-full%20wp-image-5599%22%20src=%22http://www.allen-heath.com/media/iLive_80_3Q_Main.jpg%22%20alt=%22iLive_80_3Q_Main%22%20w idth=%22770%22%20height=%22448%22%3E> (failed 2 times): DNS lookup failed: address 'www.allen-heath.com%3Cimg%20class=%22alignnone%20size-full%20wp-image-5599%22%20src=%22http' not found: [Errno 11004] getaddrinfo failed.
    • 谢谢。看起来我仍然遇到错误。 2015-04-28 18:47:02-0700 [products] WARNING: File (code: 404): Error downloading file from <GET http://www.allen-heath.com/ahproducts/ilive-112/%3Cimg%20style=%22border:%204px%20solid%20black;%22%20src=%22http://www.alleheath.com/media/ilivefx.jpg%22%20alt=%22iLive%20FX%22%20width=%22320%22%20eight=%22225%22%3E> referred in <None>
    • 呸,您的 css/xpath 完全错误,它没有提取 URL,而是提取整个元素。再次检查更新。
    猜你喜欢
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多