簡體   English   中英

除了 Python 之外,在 try 中存儲並繼續執行

[英]Store and continue the execution in try except in Python

我想存儲錯誤消息並繼續執行,但是當我使用異常 e: 存儲錯誤消息時,我無法繼續執行。 The function stops execution if URL does not get the HTTP URL, the function gives an error if

url="www.boomlive.in/fact-file/did-the-race-to-5g-cause-the-coronavirus-outbreak-7404"

我的代碼是

def twitter_link(url):
    twitter_title=[]
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    for i in soup.find_all("p"):
        try:
           for a in i.find_all("a", href=True):
              if "twitter" in a["href"]:
                 x=a["href"]
               twitter_title.append(x)
        except Exception as e:
           twitter_title.append(str(e))
           continue  

    return twitter_title

我的例外 output 是 function 將 URL 存儲在列表 twitter_title 中,如果有錯誤則存儲錯誤消息並繼續執行

function 停止執行,因為requests.get(url)拋出異常並且該調用不在 try 塊內。

暫無
暫無

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

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