首页 > 解决方案 > 使用 instapy-cli 库防止 python 脚本使用 try/except 崩溃

问题描述

我正在使用一个发布到 instagram (instapy-cli) 的 python 库。但是,这将用于安装,如果 wifi 中断,即使上传不起作用,我仍然希望脚本运行。我已经将它包装在 try/except 中,但如果函数崩溃,它仍然会结束脚本。

这是我的代码

try:
    with client(username,password) as cli:
        cli.upload(fileName, text)
except Exception as e:
    print(e.output)

在控制台中,我得到下面提到的错误:

ClientError URLError urlopen 错误 [Errno 11001] getaddrinfo failed>(代码:0,响应:)

在我有代码之前:

try:
    subprocess.call([r'filePath/insta.bat', fileName, caption])
except subprocess.CalledProcessError as e:
    print(e)

完全按照我的意愿工作(如果没有wifi,脚本会继续运行并且它会默默地失败)

标签: pythonpython-3.xexceptioninstagraminstagram-api

解决方案


我挖掘了库 instapy-cli 的源代码,在上传函数中发现:

except ClientLoginError as e:
    print('ClientLoginError {0!s}'.format(e))
    exit(9)

所以我分叉了 repo,注释掉了退出函数,卸载了库,然后重新安装了我的分叉版本。像魅力一样工作!


推荐阅读