I'm not sure the root of this problem - ultimately, I'm trying to retrieve the followers of an Instagram account ("strengthinsheets") and their number of followers.
There are no issues with the data -- however, I originally tried to use Gspread and send it right to a Google Sheet. This seemed to time out regularly.
Now, I'm just trying to print the info to a CSV -- the below script will give me the correct results (not in a CSV):
import csv
from turtle import pd
import instaloader
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36'
}
loader = instaloader.Instaloader()
loader.login("username", "password!")
profile = instaloader.Profile.from_username(loader.context, "strengthinsheets")
followees = profile.get_followees()
for followee in profile.get_followees():
print((followee.full_name, followee.username, followee.followees))
However, in order to make this work daily, I'm thinking it would be best to just save all the data to a CSV. I tried altering in this way:
import csv
from turtle import pd
import instaloader
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36'
}
loader = instaloader.Instaloader()
loader.login("username", "password!")
profile = instaloader.Profile.from_username(loader.context, "strengthinsheets")
followees = profile.get_followees()
for followee in profile.get_followees():
(followee.full_name, followee.username, followee.followees)
with open('followers.csv', 'w') as file:
writer = csv.writer(file)
writer.writerows(followee)
I'm not 100% positive I extracted to a CSV correctly, I based my code off documentation I found online, but it was confusing (I previously did it with .to_csv when pulling dataframes, but couldn't get that working this way).. I'm new to this, but would really love a file like this - it seems very attainable but if I'm wrong would love to hear it from an expert.
I don't know what is your problem ...
... but I would write it inside for
-loop using writerow()
(without char s
at the end).
with open('followers.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow("full_name", "username", "followees") # header
for followee in profile.get_followees():
writer.writerow( (followee.full_name, followee.username, followee.followees) )
Or first I would create list with all rows and later use writerows()
(with s
at the end)
all_followees = []
for followee in profile.get_followees():
all_followees.append( (followee.full_name, followee.username, followee.followees) )
with open('followers.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow("full_name", "username", "followees") # header
writer.writerows( all_followees )
and the same with DataFrame
all_followees = []
for followee in profile.get_followees():
all_followees.append( (followee.full_name, followee.username, followee.followees) )
df = pd.DataFrame(all_followees, columns=("full_name", "username", "followees"))
df.to_csv('followers.csv', index=False)