python - 跳过python中的异常
问题描述
我有一个新手问题:假设我在 python 中有这个股票清单
import requests
list = ["AMZN","APPL", "BAC"]
try:
for x in list:
url ='https://financialmodelingprep.com/api/v3/quote-short/'+x+'?apikey=demo'
response = requests.request('GET', url)
result = response.json()
print(result[0]["price"])
except:
pass
第二个股票代码将引发异常,无论第二个股票代码请求发生什么,我如何让 python 运行第三个股票代码?
解决方案
使用如下try-except
内部for
循环
import requests
list = ["AMZN","APPL", "BAC"]
for x in list:
try:
url ='https://financialmodelingprep.com/api/v3/quote-short/'+x+'?apikey=demo'
response = requests.request('GET', url)
result = response.json()
print(result[0]["price"])
except:
pass
推荐阅读
- firebase - Google 可以 Firestore 订单字符串字段,例如数字
- entity-framework-core - EF Core Cascading Delete 需要 Include,ThenInclude 尽管有映射
- ruby-on-rails - Rails 5.2 主键和外键现在是大整数了吗?什么原因?
- python - 如何在 textacy 0.6.2 中初始化“Doc”?
- java - 如何检测android应用程序中的特定声音?
- batch-file - 使用带有无人值守密码的 bat 文件解密 PGP
- json - 从 Postgresql 表中的列字段中提取键、值
- javascript - VS Code 智能感知/自动完成而不导入文件
- javascript - 添加当前状态保存的天数
- c - 信号“自动阻塞”(当执行进入处理函数时)是否会阻止另一个这样的信号被传递到另一个线程?