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

Prevent python script from crashing with try/except using instapy-cli library

我正在使用 python 发布到 instagram (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,脚本保持 运行ning 并且它无声地失败)

我深挖了库instapy-cli的源码,在上传功能中发现:

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

所以我 fork 了 repo,注释掉了 exit 函数,卸载了库,然后重新安装了我的 fork 版本。工作得很好!