如何使用scrapy检查python中的for循环中是否存在密钥

时间:2018-02-24 22:41:22

标签: python scrapy

我试图查看商家名称是否与之前的商家名称匹配,是否确实打破了迭代,如果没有继续迭代。

问题

它忽略了休息,我仍然看到重复。

run.py

 def parse(self, response):
    for business in response.css('div.info'):
        business_names = business.css('span[itemprop="name"]::text').extract()
        business_name = business.css('span[itemprop="name"]::text').extract()

        if business_name in business_names:
            break
        else:
            website = business.css('div.links  a::attr(href)').extract_first()
            phone_number = business.css('div.phones.phone.primary::text').extract()
        yield {
            'Business Name': business_names,
            'Website': website,
            'Phone Number': phone_number,

        }

1 个答案:

答案 0 :(得分:0)

使用in运算符检查是否存在,并为项目设置一些值。现在,您的代码正在检查business_name的元素是否等于business_name

for item in business_name:
    if item == business_name:
        break

要:

if 'somevaluehere' in business_names:
    break