刮痧图像,空响应[scrapy]

时间:2016-01-17 20:57:01

标签: python web-scraping scrapy

我正在使用scrapy进行图像抓取的示例。

但我没有在我的电脑上保存任何文件:

这是我正在使用的代码:

// // Items.py

import scrapy

class ImgurItem(scrapy.Item):
    title = scrapy.Field()
    image_urls = scrapy.Field()
    images = scrapy.Field()

// // settings.py

BOT_NAME = 'imgur'

SPIDER_MODULES = ['imgur.spiders']
NEWSPIDER_MODULE = 'imgur.spiders'
ITEM_PIPELINES = {'scrapy.contrib.pipeline.images.ImagesPipeline': 1}
IMAGES_STORE = '/home/ubuntu/imgurFront/'

// // imgur_spider.py

import scrapy

from scrapy.contrib.spiders import Rule, CrawlSpider
from scrapy.contrib.linkextractors import LinkExtractor
from imgur.items import ImgurItem

class ImgurSpider(CrawlSpider):
    name = 'imgur'
    allowed_domains = ['imgur.com']
    start_urls = ['http://www.imgur.com']
    rules = [Rule(LinkExtractor(allow=['/gallery/.*']), 'parse_imgur')]

    def parse_imgur(self, response):
        image = ImgurItem()
        image['title'] = response.xpath(\
            "//h2[@id='image-title']/text()").extract()
        rel = response.xpath("//img/@src").extract()
        image['image_urls'] = ['http:'+rel[0]]
        return image

这是我得到的回复类型:

{'image_urls': [u'http:data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'],
 'images': [],
 'title': []}

这些是我得到的错误:

[scrapy] ERROR: File (unknown-error): Error processing file from <GET http://i.imgur.com/BGVbmqM.jpg> referred in <None>




DEBUG: Retrying <GET http:howard-funk.jpg> (failed 1 times): Connection was refused by other side: 111: Connection refused


DEBUG: Scraped from <200

3 个答案:

答案 0 :(得分:0)

你使用过哪种版本的scrapy? 确保你有权在文件夹上书写。

在最后一种情况下,您可以创建自定义管道http://doc.scrapy.org/en/latest/topics/media-pipeline.html#custom-images-pipeline-example并捕获一些错误

答案 1 :(得分:0)

似乎比网站阻止僵尸网络连接。尝试模拟http代理(Google上的RandomUserAgentMiddleware)和/或使用TOR或代理与scrapy(settings.py上的HTTP_PROXY)。

答案 2 :(得分:0)

这里有两个问题:

  1. 建议使用urljoin获取完全限定的网址:

        image['image_urls'] = [response.urljoin(rel[0])]
    
  2. 您正在获取base64编码的图像数据。您应该使用data:image前缀跳过值,或者以不同的方式处理它们(因为这是图像文件内容,您不需要下载它)。