【问题标题】:How to scrape likes on instagram如何在 Instagram 上刮点赞
【发布时间】:2019-05-22 15:05:32
【问题描述】:

我得到了一个用于抓取 Instagram 数据的代码。这是刮掉追随者,追随者和帖子,但我仍然需要在帖子上刮点赞。有没有办法在没有 API 的情况下从 instagram 上刮点赞?

这是scrape的代码,我还需要在这里刮点赞。

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)

【问题讨论】:

  • 你能提供任何示例网址吗?

标签: python beautifulsoup instagram


【解决方案1】:

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

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 个赞

【讨论】:

    【解决方案2】:

    您可以使用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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多