2015-11-14 49 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

相關問題