如何完整获取Mastodon账号所有关注者?API限制突破方案咨询
获取Mastodon账号完整关注者列表的方法
一、通过官方API分页遍历
Mastodon的accounts/followers接口单次返回上限为80条,但可以通过分页参数迭代获取所有关注者,核心逻辑是利用max_id参数逐步翻页:
- 首次调用接口时,传入
limit=80获取前80条关注者; - 从返回结果中提取最后一条数据的
id,作为下一次请求的max_id参数; - 重复上述步骤,直到接口返回空列表,即表示已获取全部关注者。
Python实现示例(使用官方SDK mastodon.py)
from mastodon import Mastodon # 初始化客户端(需提前获取实例的访问令牌) mastodon = Mastodon( access_token='你的访问令牌', api_base_url='https://你的实例域名' ) all_followers = [] max_id = None while True: # 分页获取关注者 batch_followers = mastodon.account_followers(目标账号ID, max_id=max_id, limit=80) if not batch_followers: break all_followers.extend(batch_followers) # 更新下一页的max_id max_id = batch_followers[-1]['id'] print(f"共获取到 {len(all_followers)} 个关注者")
Python实现示例(使用原生requests库)
import requests BASE_API_URL = 'https://你的实例域名/api/v1' ACCESS_TOKEN = '你的访问令牌' TARGET_ACCOUNT_ID = '目标账号ID' all_followers = [] max_id = None headers = {'Authorization': f'Bearer {ACCESS_TOKEN}'} while True: params = {'limit': 80} if max_id: params['max_id'] = max_id # 发送请求 response = requests.get(f"{BASE_API_URL}/accounts/{TARGET_ACCOUNT_ID}/followers", headers=headers, params=params) response.raise_for_status() batch_followers = response.json() if not batch_followers: break all_followers.extend(batch_followers) max_id = batch_followers[-1]['id'] print(f"共获取到 {len(all_followers)} 个关注者")
二、关于snscrape的可行性
snscrape对Mastodon关注者列表的支持并不理想,不推荐作为首选方案:
- snscrape依赖Mastodon网页端的HTML结构,实例前端更新后抓取逻辑会直接失效;
- 部分实例存在反爬机制,会限制snscrape的请求频率;
- 无法保证数据的完整性和准确性,没有官方API那样可靠的分页迭代机制。
内容的提问来源于stack exchange,提问作者adinas
相关产品推荐
相关产品推荐

