簡體   English   中英

發生錯誤時循環不會中斷

[英]While Loop doesn't break when error is occurred

我希望我的while循環在出現錯誤時中斷,但它不會中斷/關閉程序......

def check_listing_sell():
    counter = 0
    house_counter = 0
    while True:

        url = f"https://www.remax-quebec.com/fr/courtiers-immobiliers/james.he/index.rmx?offset={counter}#listing"
        r = requests.get(url)

        try:
            soup = BeautifulSoup(r.text, "html.parser")

            for item in soup.select("div.property-address"):

                house_counter += 1
                address_prospect = item.get_text(strip=True)
                print(f"{address_prospect} {house_counter}")

            counter += 12
        except Exception as e:
            print(e)
            break


check_listing_sell()

出於某種原因,即使在“無結果”頁面上, soup.select("div.property-address")返回一個空的 web 元素(不是錯誤)。 因此,應該添加條件if len(soup.select("div.property-address")) == 0 此外,將r = requests.get(url)放在 try 塊中是一個不錯的建議。

    while True:

        url = f"https://www.remax-quebec.com/fr/courtiers-immobiliers/james.he/index.rmx?offset={counter}#listing"

        try:
            r = requests.get(url)
            soup = BeautifulSoup(r.text, "html.parser")
            
            if len(soup.select("div.property-address")) == 0:
                break

            for item in soup.select("div.property-address"):

                house_counter += 1
                address_prospect = item.get_text(strip=True)
                print(f"{address_prospect} {house_counter}")

            counter += 12
        except Exception as e:
            print(e)
            break

將調用移動到try內的requests.get()

KeyboardInterrupt不是Exception的子類型,因此您需要一個單獨的except塊。

#from bs4 import BeautifulSoup
import requests

def check_listing_sell():
    counter = 0
    house_counter = 0
    while True:
        url = f"https://www.remax-quebec.com/fr/courtiers-immobiliers/james.he/index.rmx?offset={counter}#listing"

        try:
            print(url)
            r = requests.get(url)
            print(r.text[:30])
            # soup = BeautifulSoup(r.text, "html.parser")

            # for item in soup.select("div.property-address"):

            #     house_counter += 1
            #     address_prospect = item.get_text(strip=True)
            #     print(f"{address_prospect} {house_counter}")

            counter += 12
        except KeyboardInterrupt:
            print("Manual interrupt")
            break
        except Exception as e:
            print(f"Exception occurred for counter={counter}, stopping loop: {e}")
            break

check_listing_sell()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM