Python Scrapy TypeError:无法混合str和非str参数错误
我是Python新手,我正在学习教程,我发现了这个错误 当我运行scrapy时,它会显示typeerror:无法混合str和non-str参数。您能帮我吗 先谢谢你Python Scrapy TypeError:无法混合str和非str参数错误,python,web-scraping,scrapy,typeerror,Python,Web Scraping,Scrapy,Typeerror,我是Python新手,我正在学习教程,我发现了这个错误 当我运行scrapy时,它会显示typeerror:无法混合str和non-str参数。您能帮我吗 先谢谢你 class ForRentSpider(scrapy.Spider): name = 'for_rent' allowed_domains = ['www.laforet.com/'] start_urls = ['https://www.laforet.com/louer/rechercher'] def parse(self,
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将其解释为单个值的元组。删除该行末尾的,
将解决您的问题
代码中还有其他行也这样做,如果您不想让这些变量成为元组,您应该删除它们行末尾的
,
。不过,它们不会引起问题,因为它们不在请求方法中使用。欢迎使用堆栈溢出。今后,请将您的执行日志包含在此类问题中,因为它们有助于我们理解问题
代码中的问题是,在变量link
中传递元组时,元组应该是字符串。原因是您在变量定义的末尾添加了一个逗号,没有明显的原因
link = response.urljoin(house.xpath(".//@href").get()),
Python将其解释为单个值的元组。删除该行末尾的,
将解决您的问题
代码中还有其他行也这样做,如果您不想让这些变量成为元组,您应该删除它们行末尾的,
。但是,它们不会引起问题,因为它们没有在请求方法中使用。请在您的帖子中包含错误的全文或您得到的回溯,作为格式化文本。请不要发布文本图像。请在您的帖子中包含错误的全文或您得到的回溯,作为格式化文本。请不要发布文本图像。