如何从Instagrapi列表响应中获取某些数据

发布于 2025-01-21 19:08:40 字数 1486 浏览 3 评论 0原文

因此,我正在研究一些代码,以拉出喜欢Instagram上帖子的人的照片。我正在为API系统使用Instagrapi,但是使用api.media_likers返回所需信息列表。由于某些原因,它将其返回typeError:'userShort'对象无法订阅。我不确定该怎么做,因为这里的代码:



from instagrapi import Client


insta_user = ""
insta_pass = ""

api = Client() #logs in
api.login(insta_user, insta_pass) #logs in

url = input("Please enter the url to the populer photo: ")
populer_photo_id = api.media_pk_from_url(url)   # gets media id from url given from user
print(f"got media id ==> {populer_photo_id}")

get_likes = api.media_info(populer_photo_id).dict() # gets number of likes
likes = get_likes['like_count']                     # gets number of likes


likers_res = api.media_likers(populer_photo_id) # gets the people who have liked said post (returns in list)


print(list((object['username'] for object in likers_res)))

应该从列表中返回用户名,而是返回

Please enter the url to the populer photo: https://www.instagram.com/p/xxxxxxxxxx/
got media id ==> xxxxxxxxxxxxxxxx
Traceback (most recent call last):
  File "c:\Users\gagem\Documents\Pr0j3cts\insta-promo\testing-api-file.py", line 43, in <module>
    print(list((object['username'] for object in likers_res)))
  File "c:\Users\gagem\Documents\Pr0j3cts\insta-promo\testing-api-file.py", line 43, in <genexpr>
    print(list((object['username'] for object in likers_res)))
TypeError: 'UserShort' object is not subscriptable

so I'm working on some code to pull the pictures of people who have liked a post on instagram. i'm using instagrapi for the api system, but when using api.media_likers which returns a list of needed info. for some reason it returns it with TypeError: 'UserShort' object is not subscriptable. Im not sure what to do because the code here:



from instagrapi import Client


insta_user = ""
insta_pass = ""

api = Client() #logs in
api.login(insta_user, insta_pass) #logs in

url = input("Please enter the url to the populer photo: ")
populer_photo_id = api.media_pk_from_url(url)   # gets media id from url given from user
print(f"got media id ==> {populer_photo_id}")

get_likes = api.media_info(populer_photo_id).dict() # gets number of likes
likes = get_likes['like_count']                     # gets number of likes


likers_res = api.media_likers(populer_photo_id) # gets the people who have liked said post (returns in list)


print(list((object['username'] for object in likers_res)))

This is supposed to return the username from the list but instead returns

Please enter the url to the populer photo: https://www.instagram.com/p/xxxxxxxxxx/
got media id ==> xxxxxxxxxxxxxxxx
Traceback (most recent call last):
  File "c:\Users\gagem\Documents\Pr0j3cts\insta-promo\testing-api-file.py", line 43, in <module>
    print(list((object['username'] for object in likers_res)))
  File "c:\Users\gagem\Documents\Pr0j3cts\insta-promo\testing-api-file.py", line 43, in <genexpr>
    print(list((object['username'] for object in likers_res)))
TypeError: 'UserShort' object is not subscriptable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2025-01-28 19:08:41

您只需要更改

print(list((object['username'] for object in likers_res)))

print(list((object.username for object in likers_res)))

you just need to change

print(list((object['username'] for object in likers_res)))

into

print(list((object.username for object in likers_res)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文