我正在使用一个发布到 instagram 的 python 库 (instapy-cli)。但是,这将用于安装,如果 wifi 断开,即使上传不起作用,我仍然希望脚本运行。我已将其包装在 try/except 中,但如果函数崩溃,它仍然会结束脚本。
这是我的代码
try:
with client(username,password) as cli:
cli.upload(fileName, text)
except Exception as e:
print(e.output)
在控制台中我收到以下提到的错误:
ClientError URLError urlopen error [Errno 11001] getaddrinfo failed> (Code: 0, Response: )
在我获得代码之前:
try:
subprocess.call([r'filePath/insta.bat', fileName, caption])
except subprocess.CalledProcessError as e:
print(e)
它完全按照我想要的方式工作(如果没有 wifi,脚本会继续运行并且默默地失败)
最佳答案
我深入研究了instapy-cli库的源代码并在上传功能中发现:
except ClientLoginError as e:
print('ClientLoginError {0!s}'.format(e))
exit(9)
因此,我 fork 了该存储库,注释掉了退出函数,卸载了该库,然后重新安装了我的 fork 版本。效果非常好!
关于python - 使用 instapy-cli 库通过 try/except 防止 python 脚本崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58072571/