我正在尝试使用 instaloader 包在 python 中获取用户的所有关注者。我一次可以从一个帐户中获取大约 3 万个用户名,之后提供的帐户被 Instagram 禁止。通过第一次运行脚本,我得到了 33000 个用户名,然后脚本停止工作,因为帐户被禁止了。因此,通过使用不同的帐户再次运行脚本,我想继续从存储在 .txt 文件(代码中提到)中的最后检索到的用户名中抓取。
这样做的目的也是为了节省时间和资源。有什么办法吗?
这是我的代码:
import instaloader
from datetime import datetime
from itertools import dropwhile, takewhile
import csv
class GetInstagramProfile():
def __init__(self) -> None:
self.L = instaloader.Instaloader()
def get_followers(self,user_name):
'''Note: login required to get a profile's followers.'''
USER = "user"
PASSWORD = "password"
self.L.login(USER, PASSWORD)
profile = instaloader.Profile.from_username(self.L.context, user_name)
existing_usernames = set()
try:
# Read existing usernames from file
with open("ilirlatifi_followers.txt", "r") as file:
for line in file:
existing_usernames.add(line.strip())
except FileNotFoundError:
pass # File does not exist yet, so ignore
# Append new usernames to file
with open("ilirlatifi_followers.txt","a") as file:
print("<------Writing usernames to ilirlatifi_followers.txt------>")
for username in profile.get_followers():
username = username.username
if username not in existing_usernames:
file.write(username + "\n")
if __name__=="__main__":
cls = GetInstagramProfile()
cls.get_followers("ilirlatifi")