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

时间:2019-11-21 19:37:06

标签: python web-scraping scrapy screen-scraping

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

我可以用熊猫发布进程,但是现在我想在scrapy脚本中实现。

我尝试了这个,但是出错了。

town.py”,第88行     其他:        ^ SyntaxError:语法无效

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

3 个答案:

答案 0 :(得分:0)

您可能正在弄乱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

答案 1 :(得分:0)

编辑piplines.py

import requests

with open("file.txt") as f:
    for line in f:
        user_id, email = line.split()
        response = requests.post(
            'https://companyname.jitbit.com/api/UpdateUser',
            auth=('username', 'password'),
            data=dict(
                userId=user_id,
                email=email,
            ),
        )
        print(response)

答案 2 :(得分:0)

extract_first方法具有default自变量,您可以在这种情况下使用:

value = response.xpath("selector").extract_first(default="0")

或:

value = response.xpath("selector").extract_first("0")
相关问题