简体   繁体   English

Tweepy:关于在分页响应后访问用户的“id”

[英]Tweepy: about accessing the "id" of a user after a Pagination response

I'm really stuck on this one.我真的坚持这个。

I'm using Tweepy to get the IDs of all users that liked a specific tweet.我正在使用 Tweepy 来获取所有喜欢特定推文的用户的 ID。 I seem to get a list of "User" structures that contain "id", "name" and "username", but I'm not able to get only the "id".我似乎得到了一个包含“id”、“name”和“username”的“用户”结构列表,但我不能只得到“id”。

The code is simple:代码很简单:

client = tweepy.Client(
    bearer_token=bearer_token,
    consumer_key=api_key, consumer_secret=api_secret,
    access_token=user_token, access_token_secret=user_token_secret,
    wait_on_rate_limit=True
)

for response in tweepy.Paginator(client.get_liking_users, id=tweetid, max_results=100, limit=10):
    for item in response:
        print("ITEM:\n", item)
        if item is not None:
            for user in item:
                if user is not None:
                    print(user)

The print of "item" gets me this (simplified, of course; the number of structures is high, that's why I have to use Paginator): “项目”的打印让我明白了这一点(当然是简化的;结构的数量很多,这就是我必须使用 Paginator 的原因):

[<User id=0000001 name=user1 username=UserName1>, <User id=0002 name=user2 username=UserName2>, <User id=000003 name=user3 username=UserName3>]

and the print of "user" just gets me the individual usernames: "UserName1", etc. “用户”的打印只是让我得到个人用户名:“UserName1”等。

But no way to get user.id, user.User.id, nor anything similar.但是没有办法获得 user.id、user.User.id 或任何类似的东西。 And I'm frustrated, because the information is right there, just I can't access it easily.我很沮丧,因为信息就在那里,只是我无法轻松访问它。

Thank you!谢谢!

Tweepy documentation provides an example of something very similar to what you want to do: https://docs.tweepy.org/en/stable/examples.html -> API v2 -> Get Tweet's Liking Users Tweepy 文档提供了一个与您想要做的非常相似的示例: https ://docs.tweepy.org/en/stable/examples.html -> API v2 -> Get Tweet's Liking Users

import tweepy


bearer_token = ""

client = tweepy.Client(bearer_token)

# Get Tweet's Liking Users

# This endpoint/method allows you to get information about a Tweet’s liking
# users

tweet_id = 1460323737035677698

# By default, only the ID, name, and username fields of each user will be
# returned
# Additional fields can be retrieved using the user_fields parameter
response = client.get_liking_users(tweet_id, user_fields=["profile_image_url"])

for user in response.data:
    print(user.username, user.profile_image_url)

This example prints the user's username and profile image URL, but note the comment says the id is also returned, so something like user.id should work.此示例打印用户的用户名和个人资料图片 URL,但请注意评论说 id 也返回,所以类似user.id的东西应该可以工作。 Otherwise, you can also add id to user_fields to make sure it's returned, although that shouldn't be necessary.否则,您也可以将id添加到user_fields以确保它被返回,尽管这不是必需的。

Unfortunately, I am not able to test it myself because I don't have a Twitter developer account with the required elevated access.不幸的是,我无法自己测试它,因为我没有具有所需提升访问权限的 Twitter 开发人员帐户。

Edit: I got access to an API account with elevated access and I was able to test your code, see the update below编辑:我可以访问具有提升访问权限的 API 帐户,并且能够测试您的代码,请参阅下面的更新

Iterating paginated results迭代分页结果

The reason why you need a double for loop to iterate the paginated results and it eventually crashes after showing some results with an error saying you are trying to access a non-existent id attribute on an str object is because you are not iterating the Paginator results correctly.您需要双for循环来迭代分页结果并最终在显示一些结果并显示错误说您正在尝试访问str对象上不存在的id属性后崩溃的原因是因为您没有迭代Paginator结果正确。

For the sake of simplicity, I'm going to label your three nested for loops:为简单起见,我将标记您的三个嵌套for循环:

loop 0: for response in tweepy.Paginator(...循环 0: for response in tweepy.Paginator(...
loop 1: for item in response循环1: for item in response
loop 2: for user in item循环 2: for user in item

Paginator returns a Response object with all the results in the data attribute. Paginator返回一个Response对象,其中包含data属性中的所有结果。 The object has other attributes like meta , count , etc.该对象具有其他属性,例如metacount等。

When you do loop 1, you are iterating all these data , count , etc., attributes of Response .当您执行循环 1 时,您正在迭代Response的所有这些datacount等属性。

If the attribute you are iterating happens to be the data attribute, it will start loop 2 and it will iterate the results getting the output you expect.如果您正在迭代的属性恰好是data属性,它将启动循环 2,并迭代结果以获得您期望的输出。

But loop 1 will also iterate other Reponse items outside of the data attribute.但循环 1 还将迭代data属性之外的其他Reponse项。

Let's see, for example, what happens when loop 1 enters the meta attribute.例如,让我们看看当循环 1 进入meta属性时会发生什么。

meta is a dictionary that looks like this: meta是一个看起来像这样的字典:

meta={'result_count': 80, 'next_token': '676f9b7bumw8i3jbm4nnifamw2ejjaktp8kjym6akdak9'}

When you enter loop 2 with the meta attribute, it will start iterating the keys (not the values, because that's how dicts work in Python) so the value of user in loop 2 will be either result_count or next_token .当您使用meta属性进入循环 2 时,它将开始迭代键(而不是值,因为这就是 dicts 在 Python 中的工作方式),因此循环 2 中的user的值将是result_countnext_token And it's then when you are getting your error saying you are trying to access id on a str .然后当您收到错误消息时说您正在尝试访问str上的id

What you should be doing is iterating the response.data in loop 1 instead and that will also allow removing the need of a second loop:您应该做的是在循环 1 中迭代response.data ,这也将允许消除对第二个循环的需要:

for response in tweepy.Paginator(client.get_liking_users, id=tweetid, max_results=100, limit=10):
    for user in response.data:
        print(user.id)

Edit: grammar and style编辑:语法和风格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM