【问题标题】:Scrapy Issue - TypeError: Argument must be bytes or unicode, got 'list'Scrapy 问题 - TypeError:参数必须是字节或 unicode,得到“列表”
【发布时间】:2019-01-09 00:15:27
【问题描述】:

这是我的第一个 Scrapy 蜘蛛项目。我是 Python 新手,所以请原谅我的无知。基本上,我想从 www.doritos.com/products/ 页面抓取图像。我打算将这些图像用于对象检测项目,因此我正在构建我的训练数据集。到目前为止的代码如下:

import scrapy 
from scrapy.contrib.spiders import Rule, CrawlSpider   
from scrapy.contrib.linkextractors import LinkExtractor
from doritos.items import DoritosItem                   

class DoritosSpider(CrawlSpider):
    name = 'doritos'
    allowed_domains = ['doritos.com']                                      
    start_urls = ['https://www.doritos.com']                               
    rules = [Rule(LinkExtractor(allow=['/products/.*']), 'parse_doritos')] 

    def parse_doritos(self, response):
            image = DoritosItem()
            image['title'] = response.xpath(["//img[@id='alt'/text()"]).extract() 
            rel = response.xpath('//product_thumbnail/@src').extract() 
            image['image_urls'] = ['http:'+rel[0]]                     
            return image

我检查了这些参考资料,试图拼凑出我的问题的答案。 如果答案在那里,我不会感到惊讶,但我的代码与我很难弄清楚的地方有很大的不同:

scrapy: request url must be str or unicode got list

Argument must be in bytes or unicode, got list

TypeError: argument 1 must be a string or unicode object

Argument must be bytes or unicode, got '_Element'

我从这些参考资料中了解到,我需要将函数中的参数转换为字符串。这是显示错误的回溯的屏幕截图: SCRAPY Traceback Error

我认为它位于 parse_doritos 函数的第二行,但我无法找到解决方法。如果有人可以帮助我解决这个问题,我真的很想:a)让这件事正常工作,b)了解我哪里出错了,为什么你的修复工作有效。

【问题讨论】:

  • 请将错误发布为代码,而不是图像。
  • 一般来说,截图是不能复制的,不会出现在搜索引擎结果中,经常会给手机用户带来问题。对于任何可以合理地表示为文本的内容,请避免使用它们。
  • 除此之外,这是一个很好的问题。欢迎使用 Python 和 Stack Overflow。
  • 对了,response.xpath不应该是response.selector.xpath(两次)吗?

标签: python unicode scrapy typeerror scrapy-spider


【解决方案1】:

据我所知 response.xpath - 不接受列表参数

def parse_doritos(self, response):
        image = DoritosItem()
        image['title'] = response.xpath(["//img[@id='alt'/text()"]).extract() # extra square brackets in response.xpath arguments - list argument instead of str
        rel = response.xpath('//product_thumbnail/@src').extract() # this function looks OK
        image['image_urls'] = ['http:'+rel[0]]                     
        return image

【讨论】:

    【解决方案2】:

    如果你从网站上抓取图像,我认为你必须yield 每张图像(Scrapy 会将它们存储在输出文件中)。另外,请考虑extract() 方法返回一个列表,即使您的刮板找到一个元素或根本没有找到一个元素。您可能想使用extract_first(),这不会给出一个列表,而是给出一个值(这是我能看到的唯一提供列表的地方,也许这就是问题所在)。

    编辑:在您的解析器函数中,我认为您应该生成 image dict 而不是返回它。

    【讨论】:

    • 我将函数中的以下代码更新为此并得到相同的错误:
      def parse_doritos(self, response):<br/> image = DoritosItem()<br/> image['title'] = response.xpath(["//img[@id='alt'/text()"]).extractor_first() <br/> rel = response.xpath('//product_thumbnail/@src').extractor_first() <br/> image['image_urls'] = ['http:'+rel[0]]<br/> yield image
      我是否正确实现了您的想法?
    • 抱歉,格式乱码。我什至似乎无法弄清楚降价!?!
    【解决方案3】:

    首先,如果我忘记了您的问题并且我们专注于您想要的工作,我认为这可能对下载图像非常有帮助。 here, download images in scrapy

    其次,如果我们谈论您的问题,您提取 img 源的 xpath 很好,但如果您注意到它返回 /sites/doritos.com/files/styles/product_thumbnail/public/2018-08/new-blaze.png?itok=ZFZWWSIn 这个链接,然后您只附加 http 而原始图像链接是 https://www.doritos.com/sites/doritos.com/files/styles/product_thumbnail/public/2018-08/collisions.png?itok=EZAydWWi

    您应该使用urljoin 或在前面加上https://www.doritos.com 作为 image['image_urls'] = ['https://www.doritos.com'+rel[0]]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 2021-11-14
      • 1970-01-01
      • 2020-05-05
      • 2019-11-04
      • 2020-05-27
      • 1970-01-01
      相关资源
      最近更新 更多