使用 Twython 从屏幕名称列表中获取用户 ID (Twitter API)

Using Twython to get user ID's from a list of screen names (Twitter API)

我有下面的代码,它可以根据已知 ID 的列表查询 Twitter API 的屏幕名称。我想反其道而行之:知道屏幕名称,并获取 ID。

from twython import Twython

# Paste your codes here
app_key = '...'
app_secret ='...'
oauth_token = '...-...'
oauth_token_secret= '...'

# Create twitter thing to query
twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)

# What to look up (Twitter id:s)
ids = ["259784090","136953436","1219150098"]

# Create a comma separated string from the previous list
comma_separated_string = ",".join(ids)

# Query twitter with the comma separated list
output = twitter.lookup_user(user_id=comma_separated_string)
username_list=[]

# Loop through the results (Twitter screen names)
for user in output:
    print user['screen_name']

这是相同的 API 调用,但参数不同。

The documentation for GET users/lookup says

Returns fully-hydrated user objects for up to 100 users per request, as specified by comma-separated values passed to the user_id and/or screen_name parameters

假设你上面对 Twython 的使用是正确的(我自己不使用它)你应该可以调用...

# What to look up (Twitter screen_name:s)
ids = ["john","paul","george","ringo"]

# Create a comma separated string from the previous list
comma_separated_string = ",".join(ids)

# Query twitter with the comma separated list
output = twitter.lookup_user(screen_name=comma_separated_string)

username_list=[]

# Loop through the results (Twitter screen names)
for user in output:
    print user["id_str"]