如何通过网络抓取喜欢instagram图片的用户? [英] How to web scrape users who liked an instagram picture?

查看:246
本文介绍了如何通过网络抓取喜欢instagram图片的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何从Instagram收集此类数据以进行Web抓取项目,我尝试自己使用beautifulsoup来获取它,并要求真实解析整个页面: 但这不起作用

How would I go about gathering this kind of data from Instagram for a web scraping project, I tried myself to get it using beautifulsoup and requests true parsing the whole page: but it doesn't work

import requests
from bs4 import BeautifulSoup
usrs=[]
soup=BeautifulSoup(requests.get("https://www.instagram.com/p/Bziq7f2C-jM/").content,'html.parser')
elem1=soup.find_all('div',class_="EtaWk")
#elem1 contains all the usernames within it 
if elem1:
    elem2=elem1.find('ul',class_="XQXOT")
    if elem2:
        xelems=elem2.findAll('ul',class_="Mr508")
        for i in range(len(xelems)):
            lis=xelems[i].find('a',class_="FPmhX notranslate TlrDj",title=True)
            usrs.append(a["title"])

推荐答案

如果您不是必须使用Beautifulsoup并自己解析HTML响应,则有

If it is not a requirement for you to use Beautifulsoup and parse the HTML response yourself, there is Instaloader, a Python library that allows to access Instagram very easily. After doing pip install instaloader to install it, you can do

import instaloader
L = instaloader.Instaloader()
Post = instaloader.Post.from_shortcode(L.context, 'Bziq7f2C-jM')

然后,Post.get_likes()在喜欢帖子的个人资料上返回一个迭代器,因此要打印所有用户名,您可以

Then, Post.get_likes() returns an Iterator over the Profiles that have liked the Post, so to print all the usernames, you can do

for like in Post.get_likes():
    print(like.username)

除了简单的解决方案外,Instaloader还具有自动处理限速的优势,并且支持处理登录和访问私人配置文件的帖子.

Besides being an easy solution, Instaloader has also the advantage that it handles rate limiting automatically, and that it supports handling login and accessing posts of private profiles.

这篇关于如何通过网络抓取喜欢instagram图片的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆