繁体   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