简体   繁体   English

除了 Python 之外,在 try 中存储并继续执行

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

I want to store the error message and continue the execution but when I store the error message using the except exception e: then I am not able to continue the execution.我想存储错误消息并继续执行,但是当我使用异常 e: 存储错误消息时,我无法继续执行。 The function stops execution if URL does not get the HTTP URL, the function gives an error if 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"

My code is我的代码是

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

My excepted output is function stores URL in list twitter_title, if there is an error then store the error message and continue the executions我的例外 output 是 function 将 URL 存储在列表 twitter_title 中,如果有错误则存储错误消息并继续执行

The function stops executing because the exception is thrown by requests.get(url) and that call is not inside the try-block. function 停止执行,因为requests.get(url)抛出异常并且该调用不在 try 块内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM