python - Scrapy-TypeError:不能混合 str 和非 str 参数错误
问题描述
我是 Python 新手,我正在按照教程进行操作,但出现此错误。
当我运行scrapy时,它说 typeerror:cannot mix str and non-str arguments 你能帮帮我吗
先感谢您。
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 将其解释为单个值的元组。删除,
行尾的 将解决您的问题。
您的代码中还有其他行做同样的事情,如果您不希望这些变量成为元组,您也应该删除,
它们行末尾的 。但它们不会引起问题,因为它们没有在请求方法中使用。
推荐阅读
- c# - 调整图像大小并另存为文件
- javascript - 使用 setInterval 刷新 div 但它扰乱了视图页面的模板
- c++ - 使用opencv cap无法使用While循环调用函数两次
- backup - 赛门铁克网络备份 8.1
- java - SpringBoot 中的 NoSuchElementException @ExceptionHandler 不起作用
- docker - nginx 容器:未知指令“事件”,.conf 错误
- javascript - 使用 syncfusion-react 文档编辑器查找第二次出现的文本。(Javascript)
- xml - XML 文件中的双引号
- git - 从头创建分支并从先前的提交提交
- reactjs - 如何将访问方法 dispatch() 获取到类的组件中