python - For 语句增量并跳过其余代码
问题描述
我有以下代码:
for a in range(0,len(customer_name),1):
url = "https://whatever_my_url_is.com/" + customer_name[a] + "/"
try:
page = urllib.request.urlopen(url)
except:
print("Cannot load page, bad URL.")
a = a + 1
rest of my code
它目前的作用:
- 遍历每个客户,一次一个,并加载他们的页面。
- 如果 URL 错误,我会收到
"Cannot load page, bad URL."
消息。 - 我的其余代码照常继续。
我想要它做的是立即停止它正在做的事情并移动到customer_name
列表中的下一个,而不是继续其余的代码。
解决方案
您可以使用continue
for a in range(0,len(customer_name),1):
url = "https://whatever_my_url_is.com/" + customer_name[a] + "/"
try:
page = urllib.request.urlopen(url)
except:
print("Cannot load page, bad URL.")
continue
rest of my code
推荐阅读
- python - Python:如何将命名值从 JSON 连接到字符串
- java - 无法从 jar 文件运行 Selenium 测试
- python - 如何将正则表达式应用于 Selenium/Python 中的 xpath?
- tendermint - 如何通过 /subscribe 方法使用 Tendermint websocket 获取历史事件?
- typeorm - TypeORM:使用 getMany() 时如何添加 COUNT 字段
- python - 生成子集集和未使用的元素
- javascript - 满足条件时隐藏 UI 组件
- c - 如何测试二叉树的析构函数?
- vba - 从 Excel 调用 VBA 函数 - 在选定工作表上的选定列中查找
- amazon-web-services - 放大 Auth.signIn() ClientMetadata 未发送到 Lambda 触发器