Pythonスクリプト
import datetime
import instaloader
# parameters
username = input('username: ')
logfile = 'logfile.log'
# load profile data
loader = instaloader.Instaloader()
profile = instaloader.Profile.from_username(loader.context, username)
posts = profile.mediacount
followers = profile.followers
following = profile.followees
# print results
print("Username : {}".format(username))
print("Posts : {}".format(posts))
print("Followers : {}".format(followers))
print("Following : {}".format(following))
# write results in logfile
today = datetime.date.today()
with open(logfile, 'a') as f:
print(today.isoformat(),
username, posts, followers, following, sep=',', file=f)
実行結果サンプル(標準出力)
Username : (ユーザネーム)
Posts : 7345
Followers : 609797472
Following : 41
実行結果サンプル(ファイル出力)
2023-02-27,(ユーザネーム),7345,609797472,41
pandasとmatplotlibを使ってグラフ表示してみた。
Pythonスクリプト
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import dates as mdates
from datetime import datetime as dt
# parameters
data_file = 'logfile.log'
username = input('username: ')
# data frame
df = pd.read_csv(data_file).query('user == @username')
print(df.tail())
# convert x to time
xlist = []
xlist = [dt.strptime(d, '%Y-%m-%d') for d in df[df.columns[0]]]
# plot data
plt.close()
fig, ax = plt.subplots()
ax.plot(xlist, df[df.columns[3]], marker='o')
# set labels
ax.set_ylabel("followers")
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45)
# set x-scale and format
ax.xaxis.set_major_locator(mdates.AutoDateLocator())
ax.xaxis.set_major_formatter(mdates.AutoDateFormatter("%Y-%m-%d"))
''' set x range if you need
start_time = dt.strptime('2023-2-1', '%Y-%m-%d')
end_time = dt.strptime('2023-3-31', '%Y-%m-%d')
ax.set_xlim([start_time, end_time])
'''
# show
plt.show()
実行結果
username: (user1)
date user posts followers following
21 2023-02-15 (user1) 489 268 19
22 2023-02-16 (user1) 489 279 19
23 2023-02-20 (user1) 489 280 19
26 2023-02-23 (user1) 489 290 19
28 2023-02-28 (user1) 489 291 19
使用したサンプルデータ(logfile.log)
date,user,posts,followers,following
2023-02-01,(user1),489,270,19
2023-02-02,(user2),489,271,19
2023-02-05,(user1),489,275,19
2023-02-10,(user1),489,270,19
2023-02-11,(user2),489,271,19
2023-02-15,(user1),489,268,19
2023-02-16,(user1),489,279,19
2023-02-20,(user1),489,280,19
2023-02-21,(user3),489,288,19
2023-02-22,(user2),489,286,19
2023-02-23,(user1),489,290,19
2023-02-24,(user3),489,290,19
2023-02-28,(user1),489,291,19
Comments
Post a Comment