Reputation: 49
I got a code to scrape instagram data. It's scrape the followers, following, and posts, but i still need to scrape likes on post. Is there a way to scrape likes without API from instagram?
Here's the code for scrape, i still need to scrape likes on here.
import requests
import urllib.request
import urllib.parse
import urllib.error
from bs4 import BeautifulSoup
import ssl
class Insta_Info_Scraper:
def getinfo(self, url):
html = urllib.request.urlopen(url, context=self.ctx).read()
soup = BeautifulSoup(html, 'html.parser')
data = soup.find_all('meta', attrs={'property': 'og:description'
})
text = data[0].get('content').split()
user = '%s %s %s' % (text[-3], text[-2], text[-1])
followers = text[0]
following = text[2]
posts = text[4]
info={}
info["User"] = user
info["Followers"] = followers
info["Following"] = following
info["Posts"] = posts
self.info_arr.append(info)
Upvotes: 4
Views: 6728
Reputation: 550
You can use instascrape to acquire this data with minimal code (disclaimer: I am the author of this library)
pip install using pip install insta-scrape
and then
from instascrape import Post
google_post = Post("https://www.instagram.com/p/CG0UU3ylXnv/")
google_post.load()
print(f"{google_post.likes} likes")
>>> "37210 likes"
Upvotes: 0
Reputation: 4482
Given the hypothesis that you already collected some posts url, you could easily get the likes by doing the following:
posts = ['BxuiTcLnTWO','BxkKDnCngp0','BxiNq5-nxOj','Bxhr01unQ11']
for post in posts:
post_url = 'https://www.instagram.com/p/{}/'.format(post)
response = requests.get(post_url.format(post))
soup = BeautifulSoup(response.content)
sharedData = soup.find('script', text=re.compile('"mainEntityofPage"')).text
likes = json.loads(sharedData.strip())['interactionStatistic']['userInteractionCount']
print(post_url, '-', likes, 'likes')
Output :
https://www.instagram.com/p/BxuiTcLnTWO/ - 2243387 likes
https://www.instagram.com/p/BxkKDnCngp0/ - 6278351 likes
https://www.instagram.com/p/BxiNq5-nxOj/ - 1445806 likes
https://www.instagram.com/p/Bxhr01unQ11/ - 1250237 likes
Upvotes: 4