条件语句语法

2024-04-28 07:17:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个网络爬虫,我的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()
        }

正如你所看到的,重复的次数很多,我想保持尽可能干燥。你知道吗


Tags: pathtextresponseextractresultrespxpathloc
2条回答

您可以创建字典,然后向其中添加适当的条目。你知道吗

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

你也可以通过听写理解来计算出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 }

在早期版本的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)

我会用一个查找地图:

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}

相关问题 更多 >