【问题标题】:TypeError: Request url must be str or unicode, got list:TypeError: Request url must be str or unicode, got list:
【发布时间】:2019-12-03 19:21:41
【问题描述】:

我正在尝试提取并保存图像,但每次运行蜘蛛时都会收到此错误,我在 items.py 中定义了以下函数

import scrapy   
from ..items import HamrobazarItem


class CarsSpider(scrapy.Spider):
    name = 'cars'
    start_urls = ['https://hamrobazaar.com/c48-automobiles-cars']

    def parse(self, response):
        items= HamrobazarItem()
        img_urls=list()
        img_urls.append(response.css('center img::attr(src)').extract())
        items['image_urls']=img_urls

        yield items
import scrapy

class HamrobazarItem(scrapy.Item):
    images=scrapy.Field()
    image_urls=scrapy.Field()
    pass

【问题讨论】:

  • 代码 sn-p 似乎格式错误。我从来没有用过scrapy,你确定在yield items?它产生列表,而不是列表中的项目,也许这是一个错误?
  • 我也使用了 return。但它没有工作
  • 我在这里看到 - docs.scrapy.org/en/latest/intro/tutorial.html parse() 不应该返回 URL 列表(要解析),而是产生 Request 对象:yield scrapy.Request(next_page, callback=self.parse)

标签: python web scrapy


【解决方案1】:

我无法运行您的蜘蛛,但似乎问题在于生成列表列表。 response.css('center img::attr(src)').extract() 是一个列表,img_urls.append(response.css('center img::attr(src)').extract()) 是一个列表列表,因此将其更改为 img_urls = response.css('center img::attr(src)').extract() 可能会解决您的问题。

【讨论】:

  • 最初在 settings.py 文件中有 ROBOTSTXT_OBEY = True 我将其修改为 ROBOTSTXT_OBEY = False 并且对我有用
  • 你能得到图像但无法存储它吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 2018-12-23
  • 2020-03-28
  • 2022-11-16
  • 2023-01-12
  • 1970-01-01
相关资源
最近更新 更多