#get media code media = instagram.get_media_by_id(picture.identifier) code=media.short_code #get Media likes print("likes") #not optimal to many calls likes= instagram.get_media_likes_by_code(code,5) # likes=likes['accounts'] for account in likes['accounts']: print(account.username) #get comments print("comments") comments = instagram.get_media_comments_by_code(code, 10) for comment in comments['comments']: print(comment.text) print(comment.owner) #get media from link print("get from link") media = instagram.get_media_by_url('https://www.instagram.com/p/BHaRdodBouH') print(media) print(media.owner) #get media via code print("media from code") media = instagram.get_medias_by_code('BHaRdodBouH')
#print(account.get_username()) cont = 0 if __name__ == '__main__': for media in medias: if cont == 609: #account_post_number break f = open('lilmiquela No ' + str(cont) +' .csv', 'w', encoding="utf-8") #https://www.instagram.com/p/B3P2xNAHes2/ f.write(" https://www.instagram.com/p/"+media.get_short_code()+"/ ") f.write('\n') #f.write(time_tran(media.get_media_created_time())) #f.write('\n') try: comments = instagram.get_media_comments_by_code(media.get_short_code(),10000) for comment in comments['comments']: name = comment.owner.get_username() text = comment.text created_time = time_tran(comment.created_at) f.write(name+","+text+","+created_time) f.write('\n') #f.write('\n\n\n') f.close() print(cont) time.sleep(35) if cont%5 == 0: time.sleep(74) if cont%10 == 0: time.sleep(55)
maxComms = 10000 #csvPath = os.path.join(str(os.path.join(os.path.expanduser("~"), "Desktop")), '%s-likes.csv' % postCode) csvPath = os.path.join('outputs/', '%s-comments.csv' % postCode) proxies = { "http": "socks5h://x2923101:[email protected]:1080", "https": "socks5h://x2923101:[email protected]:1080", } instagram = Instagram() instagram.set_proxies(proxies) print("\nScraping in progress... do not close this window.\n") comments = instagram.get_media_comments_by_code(postCode, maxComms) with open(csvPath, 'a', encoding='utf-8') as f: writer = csv.writer(f, lineterminator = '\n') writer.writerow(['comment_id', 'comment', 'username', 'is_private', 'is_verified']) for comment in comments['comments']: with open(csvPath, 'a', encoding='utf-8') as f: writer = csv.writer(f, lineterminator = '\n') writer.writerow([comment.identifier, comment.text, comment.owner.username, comment.owner.is_private, comment.owner.is_verified]) f.close() print ('\nDone! Successfully scraped %s comments.\n' % len(comments['comments'])) print ('A link to download the CSV data file should be below.\n')