python - 如何修复“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:'
解决方案
我认为你有这个错误,因为你试图通过“scrapy.Request”的要求传递一个列表而不是一个 str 或一个 unicode
尝试这个:
def start_requests(self):
url = 'https://www.goodreads.com/quotes?page=1'
yield scrapy.Request(url=url, callback=self.parse)
它应该工作。
推荐阅读
- arrays - 当没有目标总和匹配时,两个数字 Sum_Test 用例失败
- css - 为什么过渡后还有背景?
- mysql - 将聚合与自连接一起使用
- angular - Angular ngIf else 在 ng-container 中不起作用
- javascript - 当我运行我的代码时,它会弹出:Event { isTrusted: [Getter] } 我该怎么办?
- javascript - POST 请求在 Postman 中工作但不在浏览器中(MongoDB + ReactJS)
- javascript - NUXT 应用程序设置中的 Google 登录不起作用
- flutter - 如何在颤动的 Web http 请求中添加标头?
- r - 在受控的中断时间序列中平衡 1 个处理组和 2 个对照组之间的协变量以进行下游分析
- python - 分组附近的轮廓/边界矩形