python - Scrapy 不下载图像并出现管道错误

标签 python scrapy

我有这个代码

class MyImagesPipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
            for image_url in item['image_urls']:
                yield Request(image_url)

这是 BaseSpider 的子类蜘蛛。这只蜘蛛给我带来了噩梦

def parse(self, response):

    hxs = HtmlXPathSelector(response)
    sites = hxs.select('//strong[@class="genmed"]')
    items = []


    for site in sites[:5]:

        item = PanduItem()
        item['username'] = site.select('dl/dd/h2/a').select("string()").extract()
        item['number_posts'] = site.select('dl/dd/h2/em').select("string()").extract()
        item['profile_link'] = site.select('a/@href').extract()



        request =  Request("http://www.example/profile.php?mode=viewprofile&u=5",
        callback = self.parseUserProfile)
        request.meta['item'] = item
        return request

 def parseUserProfile(self, response):

        hxs = HtmlXPathSelector(response)
        sites = hxs.select('//div[@id="current')
        myurl = sites[0].select('img/@src').extract()

        item = response.meta['item']

        image_absolute_url = urljoin(response.url, myurl[0].strip())
        item['image_urls'] = [image_absolute_url]

        return item

这是我收到的错误。我找不到。看起来像是正在获取元素,但我不确定

ERROR

File "/app_crawler/crawler/pipelines.py", line 9, in get_media_requests
            for image_url in item['image_urls']:
        exceptions.TypeError: 'NoneType' object has no attribute '__getitem__'

最佳答案

您的 pipelines.py 中缺少一个方法 该文件包含3个方法:

  • 处理项目
  • 获取媒体请求
  • item_completed

item_completed 方法负责将图像保存到指定路径。该路径在settings.py中设置如下:

ITEM_PIPELINES = ['scrapy.contrib.pipeline.images.ImagesPipeline']
IMAGES_STORE = '/your/path/here'

如上所示,settings.py 中还包含启用 imagepipeline 的行。

我尝试以我能理解的最好方式来解释它。如需进一步引用,请查看官方scrapy documentation .

关于python - Scrapy 不下载图像并出现管道错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13880765/

相关文章:

Python动态函数属性

python - 发送消息时禁用登录 Flask-Mail

python - 如何将 itertools.product 应用于列表列表的元素?

python - 在 Python 中按任意数量的条件排序

python - 无法访问位于目标元素之外的某些文本

python - 拒绝 scrapy linkextractor 中的某些链接

python - 在 FT2Font : Can not load face

python - 抓取选择具有多个属性的<a>元素

Scrapy 请求+响应+下载时间

python-2.7 - python.failure.Failure OpenSSL.SSL.Scrapy 中的错误(版本 1.0.4)