簡體   English   中英

使用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調用,但參數不同。

GET用戶/查找文檔說明

根據傳遞給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"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM