使用instagrapi库调用Instagram API时出现ProxyError故障求助
近期开始学习使用instagrapi库,前几天发布Stories和Reels一切正常,但之后出现连接错误。从一开始就使用代理且运行正常,如今却无法连接,已尝试多款付费代理仍未解决。
报错信息
instagrapi.exceptions.ClientConnectionError: ProxyError HTTPSConnectionPool(host='i.instagram.com', port=443): Max retries exceeded with url: /api/v1/launcher/sync/ (Caused by ProxyError('Cannot connect to proxy.', TimeoutError(10060, '连接尝试失败,因为未在规定时间内收到另一台计算机的响应,或已建立的连接因对方错误响应而终止', None, 10060, None)))
相关代码
from instagrapi import Client from instagrapi.types import Usertag, Location import random import config import time class LikePost: def __init__(self, client): self.cl = client self.tags = ['arduino'] self.like_medias = [] def get_post_id(self): medias = cl.hashtag_medias_recent(random.choice(self.tags), amount=1) media_dict = medias[0].dict() return str(media_dict['id']) def like_post(self, amount): for i in range(amount): random_post = self.get_post_id() if random_post in self.like_medias: pass else: self.cl.media_like(media_id=random_post) self.like_medias.append(random_post) random_daley = random.randint(20, 60) time.sleep(random_daley) class MadeContent: def __init__(self, client): self.cl = client def made_post(self, image, text, users, post_location): self.cl.photo_upload( path=image, caption=text, usertags=users, location=post_location, extra_data={ "like_and_views_counts_disabled": False, "disacled_comments": False }) def made_photo_story(self, image): self.cl.photo_upload_to_story(image) def made_video_story(self, video): self.cl.video_upload_to_story(video) def made_reals(self, video_path, text, thumbnail_path): self.cl.clip_upload( video_path, text, thumbnail_path, ) proxy_login = "" proxy_password = "" proxy_port = "154.30.136.183:8000" proxy = f"http://{proxy_login}:{proxy_password}@{proxy_port}" cl = Client() cl.set_proxy(proxy) cl.login(config.username, config.password) content = MadeContent(cl) content.made_photo_story(r"C:\Users\PREDATOR\develop\insta_bot\R7O8r.jpg")
验证代理本身可用性:脱离instagrapi,用基础工具测试代理能否访问Instagram接口。比如用requests写测试代码:
import requests proxy = "http://{你的代理账号}:{代理密码}@{代理地址}:{端口}" try: response = requests.get("https://i.instagram.com/api/v1/launcher/sync/", proxies={"https": proxy, "http": proxy}, timeout=10) print(response.status_code) except Exception as e: print(e)如果测试失败,说明代理节点可能被Instagram封禁、服务商故障或端口/认证有误,直接联系代理服务商排查。
匹配代理协议类型:如果使用的是socks5代理,代码中要将代理前缀从
http://改为socks5://,否则无法正常连接。处理代理认证特殊字符:若代理账号或密码包含@、:等特殊字符,需进行URL编码,避免解析错误:
from urllib.parse import quote proxy_login_encoded = quote(proxy_login) proxy_password_encoded = quote(proxy_password) proxy = f"http://{proxy_login_encoded}:{proxy_password_encoded}@{proxy_port}"延长超时时间:默认超时时间可能过短,可初始化Client时设置更长超时:
cl = Client(timeout=30) # 从默认10秒调整为30秒检查账号状态:用代理在浏览器手动登录Instagram,确认账号是否被限制、需要验证,避免因账号问题导致请求被拦截。
更新instagrapi版本:旧版本可能存在代理相关bug,执行
pip install --upgrade instagrapi更新到最新版。
内容的提问来源于stack exchange,提问作者Михаил Серебряков

