在社交媒体时代,Instagram 成为了许多人展示自我、建立社交网络的重要平台。然而,许多用户在使用 Instagram 时会遇到一个令人头疼的问题:如何知道哪些人你关注了,但他们却没有关注你?手动检查关注列表和粉丝列表不仅耗时,还容易遗漏。这种情况下,一个能够自动分析 Instagram 关注关系的工具显得尤为重要。
假设你是一位社交媒体营销人员,负责管理一个品牌账号。你希望通过分析关注者和被关注者的关系,优化账号的互动策略。手动检查这些信息显然不现实,但有了 main.py
这个 Python 脚本,你就可以轻松实现这一目标。
main.py
是一个基于 Python 的脚本,利用 instaloader
库来分析 Instagram 账号的关注关系。以下是代码的详细解析:
# Made by Maxim Iliouchenko (https://github.com/maxily1)
# Importing Libraries
import instaloader
import argparse
# Get instance
L = instaloader.Instaloader()
# Creating an argument parser
parser = argparse.ArgumentParser(description='Process log-in data')
# Adding arguments
parser.add_argument('-u', type=str, required=True, help="Enter a username which will be used in the app.")
parser.add_argument('-p', type=str, required=True, help="Enter a password which will be used in the app.")
# Parsing the args
args = parser.parse_args()
# Defining the args into variables
user_name = args.u
pass_word = args.p
# Login data and load session
L.login(user_name, pass_word)
# Obtaining profile metadata
profile = instaloader.Profile.from_username(L.context, username=user_name)
# Making a list with the usernames of the followers
followers_list = []
for follower in profile.get_followers():
username = follower.username
followers_list.append(username)
# Making a list with all of the people who you're following
following_list = []
for following in profile.get_followees():
username = following.username
following_list.append(username)
# Find people who don't follow back
not_following_back = []
for following in following_list:
if following not in followers_list:
not_following_back.append(following)
print(not_following_back)
choice = input("Would you like to save the people who don't follow you as a file? (y/n): ")
if choice == "y":
f = open("dont_follow_back.txt", "w")
for person in not_following_back:
f.write(person + "\n")
else:
exit
导入库
import instaloader
:instaloader
是一个用于下载和分析 Instagram 数据的 Python 库。import argparse
:argparse
是 Python 的标准库,用于解析命令行参数。创建实例
L = instaloader.Instaloader()
:创建 instaloader
的实例,用于后续的 Instagram 数据操作。
解析命令行参数
parser = argparse.ArgumentParser(description='Process log-in data')
:创建一个命令行参数解析器。parser.add_argument('-u', type=str, required=True, help="Enter a username which will be used in the app.")
:添加用户名参数。parser.add_argument('-p', type=str, required=True, help="Enter a password which will be used in the app.")
:添加密码参数。args = parser.parse_args()
:解析命令行参数。登录和获取数据
L.login(user_name, pass_word)
:使用提供的用户名和密码登录 Instagram。profile = instaloader.Profile.from_username(L.context, username=user_name)
:获取当前登录用户的个人资料。获取关注列表和粉丝列表
followers_list
:通过 profile.get_followers()
获取粉丝列表。following_list
:通过 profile.get_followees()
获取关注列表。分析未关注回的用户
not_following_back
列表中。输出结果并保存
dont_follow_back.txt
中。对于品牌或营销人员来说,了解哪些用户没有关注回可以帮助优化营销策略。例如,可以定期运行此脚本,分析关注者的变化趋势,从而决定是否需要调整互动策略或取消关注某些用户。
对于个人用户,此脚本可以帮助清理关注列表,删除那些不互动的用户。此外,还可以扩展功能,分析互动率、评论和点赞情况,以更好地管理个人账号。
main.py
是一个基于 Python 和 instaloader
库的实用工具,能够高效地分析 Instagram 账号的关注关系。通过简单的命令行输入,用户可以快速识别未关注回的用户,并选择保存结果。该脚本不仅适用于个人账号管理,还可以扩展应用于社交媒体营销分析等多种场景。对于需要优化 Instagram 互动的用户来说,这是一个非常有价值的工具。
完整代码已开源,包含详细的注释文档:
[GitCode仓库] https://gitcode.com/laonong-1024/python-automation-scripts
[备用下载] https://pan.quark.cn/s/654cf649e5a6 提取码:f5VG