具有相似模式的多个if语句

时间:2019-06-06 10:29:22

标签: python scrapy

我有下面的代码有效,但是我正在寻找一种以更pythonic的方式编写代码的方法

    if item['merchant_1']=='Google' and not item['merchant_1_price']:
        yield scrapy.Request(url=item['merchant_1_link'],callback=self.parse_google,meta={'item': item})
    elif item['merchant_2']=='Google' and not item['merchant_2_price']:
        yield scrapy.Request(url=item['merchant_2_link'],callback=self.parse_google,meta={'item': item})

    elif item['merchant_1']=='Amazon' and not item['merchant_1_price']:
        yield scrapy.Request(url=item['merchant_1_link'],callback=self.parse_amazon,meta={'item': item})        
    elif item['merchant_2']=='Amazon' and not item['merchant_2_price']:
        yield scrapy.Request(url=item['merchant_2_link'],callback=self.parse_amazon,meta={'item': item})

    elif item['merchant_1']=='Ebay' and not item['merchant_1_price']:
        yield scrapy.Request(url=item['merchant_1_link'],callback=self.parse_ebay,meta={'item': item})
    elif item['merchant_2']=='Ebay' and not item['merchant_2_price']:
        yield scrapy.Request(url=item['merchant_2_link'],callback=self.parse_ebay,meta={'item': item})
    # another 30 similar elif statements for different sites

def parse_google(self,response):
    #code
def parse_amazon(self,response):
    #code
def parse_ebay(self,response):
    #code

我得到了两个商人(可能有价格也可能没有价格),他们肯定会有一个链接,如果他们中的任何一个都没有价格,它应该产生各自的parse_seller(亚马逊,谷歌,ebay, ...)。我以相似的模式编写了所有解析方法,以编写外观更好的(Pythonic)代码。 我正在寻找一种更紧凑的方式编写这些if语句

2 个答案:

答案 0 :(得分:2)

一种方法是使用简单的列表。

companies = ["Google", "Amazon", "Ebay"]
for company in companies:
    for i in range(1, 3):
        if item[f"merchant_{i}"] == company and not item[f"merchant_{i}_price"]:
            yield scrapy.Request(url=item[f"merchant_{i}_link"],callback=getattr(self, f"parse_{company.lower()}"),meta={'item': item})

答案 1 :(得分:0)

我不知道这是否更pythonic, 但您可以对项目进行一些重组: 现在您有:

item = { merchant1 : Google,
         merchant2 : Amazon,
         merchant_1_price: XXX,
         merchant_2_price : XXX,
         merchant_1_link : url1,
         merchant_2_link : url2,
}


您可以通过以下方式更改它:

item = { merchants: [Google, Amazon],
         prices : [xxx,xxx],
         links : [url1, url2]
}

Google和Amazon是商户对象,将通过名称和特定的解析方法进行定义 然后只需要遍历它:

for index,merchant in enumerate( item['merchants'] ):
    if not item['prices'][index]:
        yield scrapy.Request(url=item['links'][index],callback=merchant.parse,meta={'item': item})

当然,您必须将不存在的奖励设置为0或“无”。

以下是有关如何使用特定的parse_method绑定Merchant对象的示例:


class Merchant:

    def __init__(self, name, parse_method):
        self.name = name
        self.parse_method = parse_method

    def parse(self,data):
        return self.parse_method(data)


def parse_method_1(data):
    return data.split(":")

def parse_method_2(data):
    return data.split(",")

google = Merchant("Google", parse_method_1)
print(google.parse("hello:world"))

amazon = Merchant("Amazon", parse_method_2)
print(google.parse("hello,world"))

相关问题