【问题标题】:Request url must be str or unicode, got %s:' % type(url).__name__请求 url 必须是 str 或 unicode,得到 %s:' % type(url).__name__
【发布时间】:2019-04-08 11:29:30
【问题描述】:

我编写了一个简单的蜘蛛来检索引号信息:

import scrapy

class GoodReadsSpider(scrapy.Spider):
    #identity
    name = 'goodreads'

    #requests
    def start_requests(self):
        url = "https://www.goodreads.com/quotes?page=1",
        yield scrapy.Request(url=url, callback= self.parse)

    #response
    def parse(self, response):
        for quote in response.selector.xpath("//div[@class='quote']"):
            yield {
                'text': quote.xpath(".//blockquote[@class='quoteBody']/text()[1]").extract_first(),
                'author': quote.xpath(".//span[@class='quoteAuthor']/text()").extract_first(),
                'tag': quote.xpath(".//div[@class='quoteTags']/a/text()").extract(),
            }

当我运行它时,我得到以下错误:

Request url must be str or unicode, got %s:' % type(url).__name__

有人知道为什么吗?

【问题讨论】:

  • 因为你的 URL 是一个tuple,因为它的定义中有尾随逗号。实际消息应该告诉您这一点 - 您发布的是引发该错误的行,而不是结果消息。

标签: python-3.x web-scraping request scrapy


【解决方案1】:

在您的start_requests for url 中,行尾有逗号,因此它认为url 是一个元组。

def start_requests(self):
    url = "https://www.goodreads.com/quotes?page=1",  # <- remove comma here
    yield scrapy.Request(url=url, callback= self.parse)

【讨论】:

  • 谢谢!多么容易的错误:D
猜你喜欢
  • 2016-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2016-11-12
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
相关资源
最近更新 更多