Python code to scrape IG photo (url)'s comments & likes

ynrrk

Newbie
Joined
Mar 6, 2018
Messages
1
Reaction score
0
Hey, BHW

I am looking for Python code that can scrape multiple public IG photos (using the links to the photos) and get the following info:

- list of usernames who liked the photo
- list of usernames who commented on the photo, including what they commented and specifically counting the number of words in the comment

I've searched GitHub, but I haven't found exactly what I'm looking for. I know I should probably use Scrapy, but I'm not sure how to implement the specifics of what I want. I have also been researching this for months, and I am stuck. Sorry if this is a rudimentary question.

I appreciate any and all help. I have just a bit of knowledge of python.

Thanks in advance!
 
Were you able to find anything?
 
You can search instagram-api-private .
It can do which you want.
Below is example. You can try it.

from instagram_private_api import Client

api = Client('name','pass',timeout=30,cookie=False)

listHashtag =[]
listUser =[]
mapUserCount = {}
mapUserIDName = {}
mapHashtagCount = {}
listMedia = []

fileRead = open('testdata.txt','r')
listName = fileRead.readlines()
listName = [x.strip() for x in listName]
fileRead.close()
print listName

listHashtag = listName
try:
for i in range(0,len(listHashtag),1):
if i> 30:
break
hashtag = listHashtag
print hashtag
topMedia = api.feed_tag(hashtag)['ranked_items']
aaa = 0
try:
for j in range(0,len(topMedia),1):
media = topMedia[j]
if listUser.__contains__(media['user']['pk']):
mapUserCount[media['user']['pk']] = mapUserCount.get(media['user']['pk'], 0) + 1
mapUserIDName[media['user']['pk']] = media['user']['username']
else:
listUser.append(media['user']['pk'])
mapUserCount[media['user']['pk']] = 1
mediaId = media['pk']
listMedia.append(mediaId)
listUserComment = api.media_comments(mediaId)['comments']
for k in range(0,len(listUserComment),1):
userComent = listUserComment[k]
if listUser.__contains__(userComent['user']['pk']):
mapUserCount[userComent['user']['pk']] = mapUserCount.get(userComent['user']['pk'], 0) + 1
mapUserIDName[userComent['user']['pk']] = userComent['user']['username']
else:
listUser.append(userComent['user']['pk'])
mapUserCount[userComent['user']['pk']] = 1

listUserLike = api.media_likers(mediaId)['users']
aaa = aaa + len(listUserLike)
print 'so luong nguoi like la ' + str(aaa)
for k in range(0, len(listUserLike), 1):
userComent = listUserLike[k]
if listUser.__contains__(userComent['pk']):
mapUserCount[userComent['pk']] = mapUserCount.get(userComent['pk'], 0) + 1
else:
listUser.append(userComent['pk'])
mapUserCount[userComent['pk']] = 1
mapUserIDName[userComent['pk']] = userComent['username']


# inser hashtag
mediaCaption = media['caption']
mediaCaption = str(mediaCaption).split('#')
for k in range(1,len(mediaCaption)-1,1):
if listHashtag.__contains__(mediaCaption[k]):
mapHashtagCount[mediaCaption[k]] = mapHashtagCount[mediaCaption[k]] +1
else:
listHashtag.append(mediaCaption[k])
mapHashtagCount[mediaCaption[k]] = 1

print 'co so sluong hastag ' + str(len(listHashtag))
print 'co so user ' + str(len(listUser))
except:
pass
except:
pass
### write to file

filewrite = open('listHashtagRun.txt','w')
for i in range(0,len(listHashtag),1):
filewrite.write(str(listHashtag))
filewrite.write('\n')
filewrite.close()

filewrite = open('listMediaRun.txt','w')
for i in range(0,len(listMedia),1):
filewrite.write(str(listMedia))
filewrite.write('\n')
filewrite.close()

filewrite = open('listUser.txt','w')
for i in range(0,len(listUser),1):
filewrite.write(str(listUser))
filewrite.write('\n')
filewrite.close()

filewrite = open('mapUserCount.txt','w')
# for i in range(0,len(mapUserCount),1):
# filewrite.write(str(mapUserCount{i}))
# filewrite.write('\n')
filewrite.write(str(mapUserCount))
filewrite.close()

filewrite = open('mapUserName.txt','w')
# for i in range(0,len(mapUserIDName),1):
# filewrite.write(str(mapUserIDName))
# filewrite.write('\n')
filewrite.write(str(mapUserIDName))
filewrite.close()

filewrite = open('mapHashtagCount.txt','w')
# for i in range(0,len(mapHashtagCount),1):
# filewrite.write(str(mapHashtagCount))
# filewrite.write('\n')
filewrite.write(str(mapHashtagCount))
filewrite.close()

filewrite = open('dataTotal.txt','w')
for i in range(0,len(listUser),1):
filewrite.write(str(listUser))
filewrite.write(':')
filewrite.write(str(mapUserIDName.get(listUser)))
filewrite.write(':')
filewrite.write(str(mapUserCount.get(listUser)))
filewrite.write('\n')
filewrite.close()
 
Back
Top