请求网址必须是str或unicode,得到%s:'%type(url).__ name__

问题描述 投票:0回答:1

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

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__

有谁知道为什么?

python-3.x web-scraping request scrapy
1个回答
1
投票

在你的start_requestsurl你在行尾有逗号,所以它认为url是一个元组。

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