Question: I need a code to retrieve a certain Instagram account posts using API (not necessary all posts but even recent). That should be written in
I need a code to retrieve a certain Instagram account posts using API (not necessary all posts but even recent). That should be written in python.
Output in a text file. the output should show the following result:
A number of view, like and comment for each post. And post URL ( image, video, album)
--------------------------------------------------------------------------------
before i use the following code but its not working now:
import requests from InstagramAPI import InstagramAPI
class Post:
def __init__(self, media_id, media_type, media_url, media_like_count, media_comment_count, media_view_count): self.media_id = media_id self.media_type = media_type self.media_url = media_url self.media_like_count = media_like_count self.media_comment_count = media_comment_count self.media_view_count = media_view_count
all_media=[] api = InstagramAPI("sultanmis2", "Ss$9975138") api.login() # login username = "nissansaudi" response = requests.get('https://www.instagram.com/' + username + '/?__a=1') if response.status_code == 200: json = response.json() id = json['graphql']['user']['id'] print(id) total_media = api.getTotalUserFeed(id) for media in total_media: media_type = media['media_type'] if media_type == 1: media_id = media['id'] media_type = "Image" media_url = media['image_versions2']['candidates'][0]['url'] media_like_count = media['like_count'] media_comment_count = media['comment_count'] media_view_count = "none" elif media_type == 2: media_id = media['id'] media_type = "Video" media_url = media['video_versions'][0]['url'] media_like_count = media['like_count'] media_comment_count = media['comment_count'] media_view_count = "none"
else: media_id = media['id'] media_type = "Album" media_images = media['carousel_media'] media_url ='' for image in media_images: media_url = media_url + image['image_versions2']['candidates'][0]['url'] + " , " media_like_count = media['like_count'] media_comment_count = media['comment_count'] media_view_count = "none" one_post = Post(media_id, media_type, media_url, media_like_count, media_comment_count, media_view_count) all_media.append(one_post)
all_media.sort(key=lambda x: x.media_like_count, reverse=True) file_object = open('C:\\Users\\compumak\\Desktop\\intsa.txt','w') file_object.write("UserName :: " + str(username) + ' ' + "ID :: " + str(id)+ ' ' + "TotalMedia :: " + str(total_media.__len__()) ) file_object.write(" ---------------------------------------------------- ")
counter = 1; for media in all_media: line = str (counter) + ": [ MediaID= " + str(media.media_id) +" , MediaType= " + str(media.media_type) + " " line = line + " , MediaLikeCount= " + str(media.media_like_count) + " , MediaCommentCount= " + str(media.media_comment_count) + " " line = line + " , Url= " + str(media.media_url) + "] ---- "+ " " file_object.write(line) counter = counter + 1
else: print("Error" + str (response.status_code))
---------------------------------------------------------------------------------------------------
Thanks
Thanks.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
