gpt4 book ai didi

python - scrapy脚本中将空字段替换为0

转载 作者:太空宇宙 更新时间:2023-11-03 20:05:41 24 4
gpt4 key购买 nike

我正在编辑一个旧的 scrapy 脚本。对于某些页面,“浴室”字段不存在。如果不存在,我想输入一个“0”。

我可以使用 pandas 发布流程,但现在我想在 scrapy 脚本中实现此目的。

我尝试了这个,但出现错误..

town.py”,第 88 行 别的: ^语法错误:语法无效

bathrooms_txt = response.xpath(".//dt[contains(text(), 'Bathrooms')]/following-sibling::dd/text()").extract_first()
if bathrooms_txt == "":
bathrooms = "0"
else:
bathrooms = bathrooms_txt
except:
pass

最佳答案

看来您可能弄乱了您的 try 语句。

您的意思是:

bathrooms_txt = response.xpath(".//dt[contains(text(), 'Bathrooms')]/following-sibling::dd/text()").extract_first()
try:
if bathrooms_txt == "":
bathrooms = "0"
else:
bathrooms = bathrooms_txt
except:
pass

关于python - scrapy脚本中将空字段替换为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58982559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com