Python Forum
Trying to save array results to a csv - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trying to save array results to a csv (/thread-20970.html)



Trying to save array results to a csv - jos88 - Sep-08-2019

Hi,

Basically I'm trying to use the following scraper to scrape data, then save to csv however the examples given are for prints and I can't figure out how to save it instead or how I should be structuring the python file.

https://github.com/realsirjoe/instagram-scraper

Example:
https://github.com/realsirjoe/instagram-scraper/blob/master/examples/get_account_followers.py


One of my many attempts:
from igramscraper.instagram import Instagram
from time import sleep
import csv

instagram = Instagram()
instagram.with_credentials('user', 'password', '/cachepath')
instagram.login(force=False,two_step_verificator=False)

sleep(2) # Delay to mimic user
with open('test.csv', mode='w') as csv_file:
    fieldnames = ['id', 'username', 'full_name', 'bio', 'profile_pic_url', 'external_url', 'number_of_posts', 'number_of_followers', 'number_of_follows', 'is_private', 'is_verified']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()
sleep(2) # Delay to mimic user

username = 'username'
followers = []
account = instagram.get_account(username)
sleep(1)
followers = instagram.get_followers(account.identifier, 200, 100, delayed=True) # Get 150 followers of 'kevin', 100 a time with random delay between requests

for follower in followers['accounts']:
   csv_file.write(followers)
It logs in and creates the csv file okay but no idea where to go from there.

Also the results from the scraping print it out like so:
Account info:
Id: 196333333333333
Username: username
Full Name: Some Name
Bio: None
Profile Pic Url: None
External url: None
Number of published posts: -
Number of followers: 0
Number of follows: 0
Is private: False
Is verified: False
which probably isn't the best format to try and get into csv? If anyone has any ideas on how to do that bit better (or maybe I'll have to just do a Find/Replace to add commas afterwards? Think ) I'm all ears!


Thanks for any help or input on either problem above!