如何在Instagram上刮喜欢

时间:2019-05-22 15:05:33

标签: python beautifulsoup instagram

我有一个代码可以抓取instagram数据。它刮擦了追随者,关注者和帖子,但是我仍然需要在帖子上刮喜欢。如果没有来自Instagram的API,有没有办法刮掉喜欢的对象?

这是刮擦的代码,我仍然需要在此处刮擦喜欢的东西。

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)

2 个答案:

答案 0 :(得分:1)

假设您已经收集了一些帖子的网址,则可以通过执行以下操作轻松获得喜欢:

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')    

输出:

  

https://www.instagram.com/p/BxuiTcLnTWO/-2243387喜欢

     

https://www.instagram.com/p/BxkKDnCngp0/-6278351喜欢

     

https://www.instagram.com/p/BxiNq5-nxOj/-1445806喜欢

     

https://www.instagram.com/p/Bxhr01unQ11/-1250237喜欢

答案 1 :(得分:0)

您可以使用instascrape以最少的代码获取此数据(免责声明:我是该库的作者)

先使用pip install insta-scrape然后进行pip安装

from instascrape import Post 
google_post = Post("https://www.instagram.com/p/CG0UU3ylXnv/")
google_post.load()
print(f"{google_post.likes} likes")
>>> "37210 likes"