在Python中插入两行时,为什么会出现无限循环?

时间:2019-03-21 09:14:45

标签: python loops

while True:
    print ("test")
    sleep(3)
    try:
        doc = html.fromstring(page.content)
        XPATH_NAME = '//h1[@id="title"]//text()'
        XPATH_SALE_PRICE = '//span[contains(@id,"priceblock_ourprice") or contains(@id,"saleprice")]/text()'
        XPATH_ORIGINAL_PRICE = '//td[contains(text(),"List Price") or contains(text(),"M.R.P") or contains(text(),"Price")]/following-sibling::td/text()'
        XPATH_CATEGORY = '//a[@class="a-link-normal a-color-tertiary"]//text()'
        XPATH_AVAILABILITY = '//div[@id="availability"]//text()'

        RAW_NAME = doc.xpath(XPATH_NAME)
        RAW_SALE_PRICE = doc.xpath(XPATH_SALE_PRICE)
        RAW_CATEGORY = doc.xpath(XPATH_CATEGORY)
        RAW_ORIGINAL_PRICE = doc.xpath(XPATH_ORIGINAL_PRICE)
        RAw_AVAILABILITY = doc.xpath(XPATH_AVAILABILITY)
        print(RAW_SALE_PRICE)

        NAME = ' '.join(''.join(RAW_NAME).split()) if RAW_NAME else None
        SALE_PRICE = ' '.join(''.join(RAW_SALE_PRICE).split()).strip() if RAW_SALE_PRICE else None
        CATEGORY = ' > '.join([i.strip() for i in RAW_CATEGORY]) if RAW_CATEGORY else None
        ORIGINAL_PRICE = ''.join(RAW_ORIGINAL_PRICE).strip() if RAW_ORIGINAL_PRICE else None
        AVAILABILITY = ''.join(RAw_AVAILABILITY).strip() if RAw_AVAILABILITY else None
        print(SALE_PRICE)

        if not ORIGINAL_PRICE:
            ORIGINAL_PRICE = SALE_PRICE


        data = {
            'NAME': NAME,
            'SALE_PRICE': SALE_PRICE,
            'CATEGORY': CATEGORY,
            'ORIGINAL_PRICE': ORIGINAL_PRICE,
            'AVAILABILITY': AVAILABILITY,
            'URL': url,
        }

        return data
    except Exception as e:
        print
        e

这很好用。但是当我插入

if SALE_PRICE.startswith('EUR'):
                SALE_PRICE = SALE_PRICE.replace('EUR ', '')

在Lins上,我将RAW变量剥离为普通变量(NAME,SALE_PRICE)之后,我将以infitine循环结尾-为什么?

2 个答案:

答案 0 :(得分:0)

查看行:

DateTime

在此可以为SALE_PRICE分配一个DateTimeOffsets值。 添加行时,有时会尝试对None值执行SALE_PRICE = ' '.join(''.join(RAW_SALE_PRICE).split()).strip() if RAW_SALE_PRICE else None。这将导致异常。在您的exception子句中,您不会中断循环,它将永远持续下去。

在您的例外条款中添加None.startswith

答案 1 :(得分:0)

在try循环之外进行try catch块,换句话说,将其嵌套在try中,诸如此类

try:
  while(true):
    #All your code
except Exception as e:
   print e