You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地正常运行的Instagram账号监控代码部署VPS后报错求助

问题排查与解决方案

1. 网络环境与IP限制

  • Instagram对VPS IP存在拦截机制,尤其是廉价VPS的IP常被纳入反爬黑名单。先在VPS执行curl -I https://www.instagram.com测试连通性,返回状态码200才表示正常访问。
  • 若访问被限制,可更换VPS IP,或给instagramy配置代理(需修改库的请求参数,或设置系统级代理环境变量)。

2. 依赖版本与运行环境

  • 本地与VPS的Python版本、依赖库版本不一致是常见问题:
    • 本地执行pip freeze > requirements.txt导出依赖清单
    • VPS上执行pip install -r requirements.txt安装完全一致的依赖版本
  • 注意:exe是Windows专属格式,如果VPS是Linux系统,别用Windows打包的exe,直接运行Python脚本,或用pyinstaller在Linux环境打包成可执行文件。

3. Instagram反爬与访问频率

  • instagramy无登录状态下的API请求有严格频率限制,VPS上频繁请求极易触发限流:
    • 改用登录模式初始化,传入你的Instagram会话ID:user = InstagramUser(xuser, sessionid="你的会话ID")
    • 延长请求间隔,把time.sleep(20)改成60秒以上,降低触发反爬的概率。

4. 代码逻辑问题修正

  • 原代码中bot.polling()被死循环阻塞,永远无法执行,这会导致TeleBot的消息处理功能失效,VPS环境下可能引发隐性异常:
    import threading
    
    def monitor_instagram():
        count = 0
        while True:
            time.sleep(60)
            now = datetime.datetime.now()
            count += 1
            name = user.fullname
            bio = user.biography
            followers = user.number_of_followers  # 修正拼写错误
            followings = user.number_of_followings
            posts = user.number_of_posts
            picture_url = user.profile_picture_url
            privateornot = user.is_private  # 修正拼写错误
            message = '账号信息\n名称: {}\n简介: {}\n粉丝数: {} 关注数: {} 帖子数: {}\n是否私密: {}\n头像链接: {}\n\n消息编号: {}'.format(name,bio,followers,followings,posts,privateornot,picture_url,count)
            bot.send_message(5450253810, message)
    
    # 启动监控线程,避免阻塞bot.polling()
    threading.Thread(target=monitor_instagram, daemon=True).start()
    bot.polling()
    
  • 顺便修正了变量拼写错误:follorwers→followers、privetornot→privateornot,避免潜在数据异常。

5. 异常捕获与日志排查

  • 在VPS运行时添加异常捕获,打印错误日志方便定位问题:
    def monitor_instagram():
        count = 0
        while True:
            try:
                time.sleep(60)
                # 监控逻辑...
                bot.send_message(5450253810, message)
            except Exception as e:
                error_msg = f"[{datetime.datetime.now()}] 监控出错: {str(e)}"
                print(error_msg)
                bot.send_message(5450253810, error_msg)
                time.sleep(300)  # 出错后暂停5分钟再重试
    

内容的提问来源于stack exchange,提问作者0x0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 20:46:01