2015-11-14 46 views
0

我有下面的代码,它基于已知ID的列表查询Twitter API的屏幕名称。我想反过来:了解屏幕名称,并获取ID。使用Twython从屏幕名称列表(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'] 

回答

3

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

The documentation for GET users/lookup says

返回作为由传递给USER_ID和/或SCREEN_NAME参数

假设你上述逗号分隔值指定为每个请求最多100个用户完全水合的用户对象,使用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"] 
+0

是的!这工作完美。 – textnet

相关问题