简体   繁体   English

条件语句语法

[英]Conditional Statement Syntax

I'm writing a web crawler at the moment and my Python is rusty as hell, so I'm simply wondering whether or not there is a shorter syntax to accomplish the following... 我现在正在编写一个Web搜寻器,而我的Python像地狱一样生锈,所以我只是想知道是否有一个较短的语法来完成以下工作...

def parse(self, response):
    prc_path = '//span[@class="result-meta"]/span[@class="result-price"]/text()'
    sqf_path = '//span[@class="result-meta"]/span[@class="housing"]/text()'
    loc_path = '//span[@class="result-meta"]/span[@class="result-hood"]/text()'
    prc_resp = response.xpath(prc_path).extract_first()
    sqf_resp = response.xpath(sqf_path).extract_first()
    loc_resp = response.xpath(loc_path).extract_first()
    if sqf_resp and loc_resp:
        yield {
            'prc': response.xpath(prc_path).extract_first(),
            'sqf': response.xpath(sqf_path).extract_first(),
            'loc': response.xpath(loc_path).extract_first()
        }
    elif sqf_resp:
        yield {
            'prc': response.xpath(prc_path).extract_first(),
            'sqf': response.xpath(sqf_path).extract_first()
        }
    else:
        yield {
            'prc': response.xpath(prc_path).extract_first(),
            'loc': response.xpath(loc_path).extract_first()
        }

As you can see, there is quite a bit of repetition and I'd like to remain as DRY as possible. 如您所见,有很多重复,我想保持尽可能的干燥。

You can create the dictionary and then add the appropriate entries to it. 您可以创建字典,然后向其中添加适当的条目。

result = { 'prc': response.xpath(prc_path).extract_first() }
if sqf_path:
    result['sqf'] = response.xpath(sqf_path).extract_first()
if loc_path:
    result['loc'] = response.xpath(loc_path).extract_first()
yield result

You could also factor out the extract_path bit with a dict comprehension. 您也可以使用dict理解来extract_path位。

result = { 'prc': prc_path, 'sqf': sqf_path, 'loc': loc_path }
yield { key : response.xpath(value).extract_first()
          for (key, value) in result.items() if value }

In earlier versions of Python, this would be: 在早期版本的Python中,这将是:

result = { 'prc': prc_path, 'sqf': sqf_path, 'loc': loc_path }
yield dict((key, response.xpath(value).extract_first())
          for (key, value) in result.items() if value)

I'd go with a lookup map: 我会去查找地图:

def parse(self, response):
    # initialize your prc_path/sqf_path/loc_path here
    lookup_map = {"prc": prc_path, "sqf": sqf_path, "loc": loc_path}  # add as many as needed
    return {k: response.xpath(v).extract_first() for k, v in lookup_map.items() if v}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM