python - 使用 instapy-cli 库通过 try/except 防止 python 脚本崩溃

标签 python python-3.x exception instagram instagram-api

我正在使用一个发布到 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/

相关文章:

python - 杀死线程不起作用

python - 当iterable包含数百万个元素时,是否有zip(* iterable)的替代方法?

mysql - 为什么这个错误不是异常

Python - 抓取时出现 BeautifulSoup 错误

mysql - 在函数中使用 mysql SELECT 时遇到问题

java - 当我尝试用 Java 保存屏幕截图时出现异常

Java - System.setOut 不保存异常消息

python - pytesseract Windows错误: [Error 5] Access is denied

使用 numba prange 封装 for 循环的 Python 并行化。为什么不工作

python - 以负步幅到达序列开头的扩展切片