上述代码添加异常处理
时间: 2023-11-12 09:12:06 浏览: 40
好的,下面是添加异常处理的代码示例:
```python
try:
# 尝试执行的代码块
result = 10 / 0
except ZeroDivisionError:
# 发生 ZeroDivisionError 异常时执行的代码块
print("除数不能为0!")
except:
# 发生其他异常时执行的代码块
print("发生了其他异常!")
finally:
# 不管是否发生异常都会执行的代码块
print("程序执行完毕。")
```
在上述代码中,`try`语句包含了我们要尝试执行的代码块,如果该代码块中发生了异常,则会跳转到相应的`except`语句中执行异常处理代码块。如果没有发生异常,则跳过所有`except`语句,直接执行`finally`语句块中的代码。
在`except`语句中,我们可以根据不同的异常类型编写不同的异常处理代码块。如果没有指定具体的异常类型,则会捕获所有类型的异常。
`finally`语句块中的代码无论是否发生异常都会被执行,通常用于释放资源或清理临时数据等工作。
相关问题
上述代码如何加入异常情况处理代码
你可以使用try-except块来处理异常情况。以下是在代码中添加异常处理的示例:
```python
import re
import requests
headers = {'User-Agent':
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"}
def baidu(company, page):
num = (page-1) * 10
url = 'http://www.baidu.com/s?rtt=4&bsst=1&cl=2&tn=news&rsv_dl=ns_pc&word=' + company + '&pn=' + str(num)
res = requests.get(url, headers=headers).text
p_href = re.compile('<h3 class="news-title_1YtI1 "><a href="(.*?)"', re.S)
p_title = re.compile('class="news-title-font_1xS-F" aria-label="(.*?)"', re.S)
p_date = re.compile('class="c-color-gray2 c-font-normal c-gap-right-xsmall" aria-label="(.*?)"', re.S)
p_source = re.compile('<span class="c-color-gray" aria-label="(.*?)"', re.S)
try:
href = re.findall(p_href, res)
title = re.findall(p_title, res)
date = re.findall(p_date, res)
source = re.findall(p_source, res)
except Exception as e:
print("爬取过程中出现异常:", str(e))
return [], [], [], []
return href, title, date, source
companys = ['海康威视', '京东', '顺丰', '阿里巴巴', '海通证券', '恒大集团']
for company in companys:
for b in range(3):
href, title, date, source = baidu(company, b+1)
print(company + '第' + str(b+1) + '页爬取成功')
for i in range(len(href)):
print("Href:", href[i])
print("Title:", title[i])
print("Date:", date[i])
print("Source:", source[i])
print()
```
在上述代码中,我们使用try-except块来捕获异常。如果在爬取过程中出现异常,我们会打印出异常信息并返回空列表。这样可以确保即使出现异常,代码也能继续执行而不中断。
Python添加震荡异常代码
以下是一个简单的Python代码示例,用于模拟震荡异常:
```python
import time
import random
# 模拟数据采集
def collect_data():
# 生成随机数作为模拟数据
data = random.uniform(0, 1)
# 模拟数据采集时间
time.sleep(0.1)
return data
# 模拟震荡异常
def oscillation_abnormality():
# 生成随机数作为模拟异常值
abnormal_data = random.uniform(0, 1)
# 模拟异常值持续时间
time.sleep(0.5)
return abnormal_data
# 主程序
while True:
# 获取模拟数据
data = collect_data()
# 判断是否出现异常
if data > 0.8:
# 模拟震荡异常
abnormal_data = oscillation_abnormality()
# 输出异常信息
print("出现震荡异常,异常值为:", abnormal_data)
else:
# 输出正常信息
print("采集数据正常,数据值为:", data)
```
在上述代码中,`collect_data()`函数用于模拟数据采集,`oscillation_abnormality()`函数用于模拟震荡异常,主程序中通过判断采集的数据是否超过阈值来决定是否出现异常,并在出现异常时调用`oscillation_abnormality()`函数模拟异常情况。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![reg](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)