Scrapy TypeError:无法混合str和非str参数错误



我是Python的新手,我正在学习教程,但我遇到了这个错误。

当我运行scratch时,它会说typeerror:不能混合str和非str参数。你能帮我吗?

提前谢谢。

class ForRentSpider(scrapy.Spider):
name = 'for_rent'
allowed_domains = ['www.laforet.com/']
start_urls = ['https://www.laforet.com/louer/rechercher']
def parse(self, response):
houses = response.xpath("//div[@class='list-item mb-6 col-lg-6 col-xl-3']/div/a")
for house in houses:
title = house.xpath(".//div[@class='d-flex justify-content-between']/h4/text()").get(),
link = response.urljoin(house.xpath(".//@href").get()),
metre = house.xpath("normalize-space(.//div[@class='property-card__infos']/div/span/text())").get(),
room = house.xpath("normalize-space(.//div[@class='property-card__infos']/div/span[2]/text())").get(),
price = house.xpath("normalize-space(.//div[@class='property-card__infos']/span/text())").get()
yield response.follow(url=link, callback=self.parse_house, meta={'house_title': title, 'house_metrekare': metre, 'house_room': room, 'house_price': price})
def parse_house(self, response):
title = response.request.meta['house_title']
metre = response.request.meta['house_metrekare']
room = response.request.meta['house_room']
price = response.request.meta['house_price']
infos = response.xpath("//div[@class='property-content__description mb-4']")
for info in infos:
house_info = info.xpath(".//div[@class='mb-2']/text()").get()
yield{
'house_title': title,
'house_metrekare': metre,
'house_room': room,
'house_price': price,
'info': house_info
}

欢迎使用堆栈溢出。今后,请将您的执行日志包括在此类问题中,因为它们有助于我们了解问题。

代码中的问题是在变量link中传递元组,而该元组本应是字符串。原因是您在变量定义的末尾添加了一个逗号,没有明显的原因。

link = response.urljoin(house.xpath(".//@href").get()),

Python将其解释为单个值的元组。删除行末尾的,将解决您的问题。

代码中还有其他行也在做同样的事情,如果你不希望这些变量是元组,你也应该删除它们行末尾的,。不过,它们不会引起问题,因为它们没有在请求方法中使用。

最新更新