首页 > 解决方案 > 如何修复“Typeerror<'Request url must be str or unicode got %s: '>

问题描述

所以我是scrapy的新手,并创建了我的第一个蜘蛛。但我得到了类型错误。

这个蜘蛛只是从goodreads的第一页中抓取引用。这是 30 个带有标签和作者姓名的引号。

import scrapy

class Goodreadspider(scrapy.Spider):

    name = 'goodreads'

    def start_requests(self):
        url = ['https://www.goodreads.com/quotes?page=1']
        yield scrapy.Request(url=url, callback=self.parse)
    def parse(self, parse):
        for quote in response.selector.xpath("//div[@class='quote']"):
            yield{
            'text': quote.xpath("//div[@class='quoteText']/text()[1]").extract_first,
            'author': quote.xpath("//div[@class='quoteText']/child::a/text()").extract_first,
            'tags': quote.xpath("//div[@class='greyText smallText left']/a/text()").extract()
            }

typeerror<'请求的 url 必须是 str 或 unicode,得到 %s:'

标签: pythonscrapy

解决方案


我认为你有这个错误,因为你试图通过“scrapy.Request”的要求传递一个列表而不是一个 str 或一个 unicode

尝试这个:

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

它应该工作。


推荐阅读