我正在尝试将我拥有的Twitter ID列表转换为Twitter用户名 根据Twitter API文档,我可以每次调用提交多个请求。 输入的参数应以逗号分隔。
以下基本上是我要做的事情的核心 我得到的结果是我提交的三个ID之一的用户名。
有关如何改进此功能以接受多个ID的任何建议? 我有data = output [0] 仅仅因为我从lookup_user获得的结果不允许我解析json字段....
from twython import Twython
twitter = Twython(AUTH)
samplefollower = ["259784090","136953436","1219150098"]
output = twitter.lookup_user(user_id=samplefollower)
data = output[0]
print data["screen_name"]
来自上面代码的打印数据[“screen_name”]的结果显然是第一个id号jwchoi13的屏幕名称
打印输出[0]是
{u'follow_request_sent': False, u'profile_use_background_image': True, u'default_profile_image': True, u'id': 259784090, u'verified': False, u'profile_text_color': u'333333', u'profile_image_url_hddps': u'hddps://abs.twimg.com/sticky/default_profile_images/default_profile_6_normal.png', u'profile_sidebar_fill_color': u'DDEEF6', u'entities': {u'description': {u'urls': []}}, u'followers_count': 33, u'profile_sidebar_border_color': u'C0DEED', u'id_str': u'259784090', u'profile_background_color': u'C0DEED', u'listed_count': 1, u'status': {u'contributors': None, u'truncated': False, u'text': u'Prach got exactly $100 thethaiger thedaniel1402 @ Citgo hddp://t.co/9NPf3StbZr', u'in_reply_to_status_id': None, u'id': 361284421875269632, u'favorite_count': 0, u'source': u'<a href="hddp://instagram.com" rel="nofollow">Instagram</a>', u'retweeted': False, u'coordinates': {u'type': u'Point', u'coordinates': [-87.70976901, 42.05236495]}, u'entities': {u'symbols': [], u'user_mentions': [], u'hashtags': [], u'urls': [{u'url': u'hddp://t.co/9NPf3StbZr', u'indices': [56, 78], u'expanded_url': u'hddp://instagram.com/p/cSilvXSGfv/', u'display_url': u'instagram.com/p/cSilvXSGfv/'}]}, u'in_reply_to_screen_name': None, u'id_str': u'361284421875269632', u'retweet_count': 0, u'in_reply_to_user_id': None, u'favorited': False, u'geo': {u'type': u'Point', u'coordinates': [42.05236495, -87.70976901]}, u'in_reply_to_user_id_str': None, u'possibly_sensitive': False, u'lang': u'en', u'created_at': u'Sun Jul 28 00:38:13 +0000 2013', u'in_reply_to_status_id_str': None, u'place': {u'full_name': u'Skokie, IL', u'url': u'hddps://api.twitter.com/1.1/geo/id/4b3758bc32868265.json', u'country': u'United States', u'place_type': u'city', u'bounding_box': {u'type': u'Polygon', u'coordinates': [[[-87.780882, 42.003325], [-87.708661, 42.003325], [-87.708661, 42.06504], [-87.780882, 42.06504]]]}, u'contained_within': [], u'country_code': u'US', u'attributes': {}, u'id': u'4b3758bc32868265', u'name': u'Skokie'}}, u'profile_background_image_url_hddps': u'hddps://abs.twimg.com/images/themes/theme1/bg.png', u'utc_offset': -21600, u'statuses_count': 15, u'description': u'Student at Northwestern University', u'friends_count': 33, u'location': u'Evanston, IL', u'profile_link_color': u'0084B4', u'profile_image_url': u'hddp://abs.twimg.com/sticky/default_profile_images/default_profile_6_normal.png', u'following': True, u'geo_enabled': True, u'profile_background_image_url': u'hddp://abs.twimg.com/images/themes/theme1/bg.png', u'screen_name': u'jwchoi13', u'lang': u'en', u'profile_background_tile': False, u'favourites_count': 2, u'name': u'Jeongwoo Choi', u'notifications': False, u'url': None, u'created_at': u'Wed Mar 02 15:57:20 +0000 2011', u'contributors_enabled': False, u'time_zone': u'Central Time (US & Canada)', u'protected': False, u'default_profile': True, u'is_translator': False}
打印输出
[{u'follow_request_sent': False, u'profile_use_background_image': True, u'default_profile_image': True, u'id': 259784090, u'verified': False, u'profile_text_color': u'333333', u'profile_image_url_hddps': u'hddps://abs.twimg.com/sticky/default_profile_images/default_profile_6_normal.png', u'profile_sidebar_fill_color': u'DDEEF6', u'entities': {u'description': {u'urls': []}}, u'followers_count': 33, u'profile_sidebar_border_color': u'C0DEED', u'id_str': u'259784090', u'profile_background_color': u'C0DEED', u'listed_count': 1, u'status': {u'contributors': None, u'truncated': False, u'text': u'Prach got exactly $100 thethaiger thedaniel1402 @ Citgo hddp://t.co/9NPf3StbZr', u'in_reply_to_status_id': None, u'id': 361284421875269632, u'favorite_count': 0, u'source': u'<a href="hddp://instagram.com" rel="nofollow">Instagram</a>', u'retweeted': False, u'coordinates': {u'type': u'Point', u'coordinates': [-87.70976901, 42.05236495]}, u'entities': {u'symbols': [], u'user_mentions': [], u'hashtags': [], u'urls': [{u'url': u'hddp://t.co/9NPf3StbZr', u'indices': [56, 78], u'expanded_url': u'hddp://instagram.com/p/cSilvXSGfv/', u'display_url': u'instagram.com/p/cSilvXSGfv/'}]}, u'in_reply_to_screen_name': None, u'id_str': u'361284421875269632', u'retweet_count': 0, u'in_reply_to_user_id': None, u'favorited': False, u'geo': {u'type': u'Point', u'coordinates': [42.05236495, -87.70976901]}, u'in_reply_to_user_id_str': None, u'possibly_sensitive': False, u'lang': u'en', u'created_at': u'Sun Jul 28 00:38:13 +0000 2013', u'in_reply_to_status_id_str': None, u'place': {u'full_name': u'Skokie, IL', u'url': u'hddps://api.twitter.com/1.1/geo/id/4b3758bc32868265.json', u'country': u'United States', u'place_type': u'city', u'bounding_box': {u'type': u'Polygon', u'coordinates': [[[-87.780882, 42.003325], [-87.708661, 42.003325], [-87.708661, 42.06504], [-87.780882, 42.06504]]]}, u'contained_within': [], u'country_code': u'US', u'attributes': {}, u'id': u'4b3758bc32868265', u'name': u'Skokie'}}, u'profile_background_image_url_hddps': u'hddps://abs.twimg.com/images/themes/theme1/bg.png', u'utc_offset': -21600, u'statuses_count': 15, u'description': u'Student at Northwestern University', u'friends_count': 33, u'location': u'Evanston, IL', u'profile_link_color': u'0084B4', u'profile_image_url': u'hddp://abs.twimg.com/sticky/default_profile_images/default_profile_6_normal.png', u'following': True, u'geo_enabled': True, u'profile_background_image_url': u'hddp://abs.twimg.com/images/themes/theme1/bg.png', u'screen_name': u'jwchoi13', u'lang': u'en', u'profile_background_tile': False, u'favourites_count': 2, u'name': u'Jeongwoo Choi', u'notifications': False, u'url': None, u'created_at': u'Wed Mar 02 15:57:20 +0000 2011', u'contributors_enabled': False, u'time_zone': u'Central Time (US & Canada)', u'protected': False, u'default_profile': True, u'is_translator': False}, {u'follow_request_sent': False, u'profile_use_background_image': True, u'default_profile_image': False, u'id': 136953436, u'verified': False, u'profile_text_color': u'666666', u'profile_image_url_hddps': u'hddps://pbs.twimg.com/profile_images/1113190398/free-pictures-rollercoaster-superman-SpacePotato__1__normal.jpg', u'profile_sidebar_fill_color': u'252429', u'entities': {u'description': {u'urls': []}}, u'followers_count': 3, u'profile_sidebar_border_color': u'181A1E', u'id_str': u'136953436', u'profile_background_color': u'1A1B1F', u'listed_count': 0, u'status': {u'contributors': None, u'truncated': False, u'text': u'RT @ozzyzzz: \ub4dc\ub77c\ub9c8 <\ub098\uc778> \ubbf8\uad6d\uc5d0\uc11c \ub9ac\uba54\uc774\ud06c. abc \ud3b8\uc131\uc774 \uc720\ub825\ud558\ub2e4\uace0. \uc88b\uc740 \uc77c\uc785\ub2c8\ub2e4. \ud5a5\ud6c4 \uc774\ub7f0 \uc2dd\uc758 \uc7a5\ub974 \ub4dc\ub77c\ub9c8\ub4e4\uc774 \ube44\uad50\uc801 \uc548\uc815\uc801\uc778 \ud658\uacbd\uc5d0\uc11c \uc7ac\uc0dd\uc0b0\ub420 \uc218 \uc788\uae38 \uae30\ub300\ud574\ubd05\ub2c8\ub2e4. hddp://t.co/WV5MHIOdOt', u'in_reply_to_status_id': None, u'id': 394683544024649728, u'favorite_count': 0, u'source': u'<a href="hddp://twitter.com" rel="nofollow">Twitter Web Client</a>', u'retweeted': False, u'coordinates': None, u'entities': {u'symbols': [], u'user_mentions': [{u'id': 93787077, u'indices': [3, 11], u'id_str': u'93787077', u'screen_name': u'ozzyzzz', u'name': u'\ud5c8\uc9c0\uc6c5'}], u'hashtags': [], u'urls': [{u'url': u'hddp://t.co/WV5MHIOdOt', u'indices': [113, 135], u'expanded_url': u'hddp://media.daum.net/entertain/enews/newsview?newsid=20131027105206884', u'display_url': u'media.daum.net/entertain/enew\u2026'}]}, u'in_reply_to_screen_name': None, u'id_str': u'394683544024649728', u'retweet_count': 154, u'in_reply_to_user_id': None, u'favorited': False, u'retweeted_status': {u'contributors': None, u'truncated': False, u'text': u'\ub4dc\ub77c\ub9c8 <\ub098\uc778> \ubbf8\uad6d\uc5d0\uc11c \ub9ac\uba54\uc774\ud06c. abc \ud3b8\uc131\uc774 \uc720\ub825\ud558\ub2e4\uace0. \uc88b\uc740 \uc77c\uc785\ub2c8\ub2e4. \ud5a5\ud6c4 \uc774\ub7f0 \uc2dd\uc758 \uc7a5\ub974 \ub4dc\ub77c\ub9c8\ub4e4\uc774 \ube44\uad50\uc801 \uc548\uc815\uc801\uc778 \ud658\uacbd\uc5d0\uc11c \uc7ac\uc0dd\uc0b0\ub420 \uc218 \uc788\uae38 \uae30\ub300\ud574\ubd05\ub2c8\ub2e4. hddp://t.co/WV5MHIOdOt', u'in_reply_to_status_id': None, u'id': 394364791457722368, u'favorite_count': 32, u'source': u'<a href="hddp://www.apple.com/" rel="nofollow">OS X</a>', u'retweeted': False, u'coordinates': None, u'entities': {u'symbols': [], u'user_mentions': [], u'hashtags': [], u'urls': [{u'url': u'hddp://t.co/WV5MHIOdOt', u'indices': [100, 122], u'expanded_url': u'hddp://media.daum.net/entertain/enews/newsview?newsid=20131027105206884', u'display_url': u'media.daum.net/entertain/enew\u2026'}]}, u'in_reply_to_screen_name': None, u'id_str': u'394364791457722368', u'retweet_count': 154, u'in_reply_to_user_id': None, u'favorited': False, u'geo': None, u'in_reply_to_user_id_str': None, u'possibly_sensitive': False, u'lang': u'ko', u'created_at': u'Sun Oct 27 07:27:47 +0000 2013', u'in_reply_to_status_id_str': None, u'place': None}, u'geo': None, u'in_reply_to_user_id_str': None, u'possibly_sensitive': False, u'lang': u'ko', u'created_at': u'Mon Oct 28 04:34:24 +0000 2013', u'in_reply_to_status_id_str': None, u'place': None}, u'profile_background_image_url_hddps': u'hddps://abs.twimg.com/images/themes/theme9/bg.gif', u'utc_offset': -18000, u'statuses_count': 5, u'description': u'', u'friends_count': 82, u'location': u'', u'profile_link_color': u'2FC2EF', u'profile_image_url': u'hddp://pbs.twimg.com/profile_images/1113190398/free-pictures-rollercoaster-superman-SpacePotato__1__normal.jpg', u'following': False, u'geo_enabled': False, u'profile_background_image_url': u'hddp://abs.twimg.com/images/themes/theme9/bg.gif', u'screen_name': u'djdoh88', u'lang': u'en', u'profile_background_tile': False, u'favourites_count': 0, u'name': u'Doh Y Jung', u'notifications': False, u'url': None, u'created_at': u'Sun Apr 25 10:53:47 +0000 2010', u'contributors_enabled': False, u'time_zone': u'Eastern Time (US & Canada)', u'protected': True, u'default_profile': False, u'is_translator': False}, {u'follow_request_sent': False, u'profile_use_background_image': True, u'default_profile_image': False, u'id': 1219150098, u'verified': False, u'profile_text_color': u'333333', u'profile_image_url_hddps': u'hddps://pbs.twimg.com/profile_images/3306983555/b45937c4656908b89ce0efbc09ecbf2f_normal.png', u'profile_sidebar_fill_color': u'EFEFEF', u'entities': {u'description': {u'urls': []}}, u'followers_count': 23, u'profile_sidebar_border_color': u'EEEEEE', u'id_str': u'1219150098', u'profile_background_color': u'131516', u'listed_count': 1, u'status': {u'contributors': None, u'truncated': False, u'text': u'#freechampagne at #themusketroom I love this place already hddp://t.co/6zoDsyWSek', u'in_reply_to_status_id': None, u'id': 396056308862160896, u'favorite_count': 0, u'source': u'<a href="hddp://instagram.com" rel="nofollow">Instagram</a>', u'retweeted': False, u'coordinates': None, u'entities': {u'symbols': [], u'user_mentions': [], u'hashtags': [{u'indices': [0, 14], u'text': u'freechampagne'}, {u'indices': [18, 32], u'text': u'themusketroom'}], u'urls': [{u'url': u'hddp://t.co/6zoDsyWSek', u'indices': [59, 81], u'expanded_url': u'hddp://instagram.com/p/gJm7XRKia7/', u'display_url': u'instagram.com/p/gJm7XRKia7/'}]}, u'in_reply_to_screen_name': None, u'id_str': u'396056308862160896', u'retweet_count': 0, u'in_reply_to_user_id': None, u'favorited': False, u'geo': None, u'in_reply_to_user_id_str': None, u'possibly_sensitive': False, u'lang': u'en', u'created_at': u'Thu Oct 31 23:29:17 +0000 2013', u'in_reply_to_status_id_str': None, u'place': None}, u'profile_background_image_url_hddps': u'hddps://abs.twimg.com/images/themes/theme14/bg.gif', u'utc_offset': -18000, u'statuses_count': 93, u'description': u'Born in Korea, Grew up in Jersey,\r\nWent to college in Chicago (well Chicago Suburbs)\r\nand Currently working in NYC', u'friends_count': 214, u'location': u'New York, NY', u'profile_link_color': u'009999', u'profile_image_url': u'hddp://pbs.twimg.com/profile_images/3306983555/b45937c4656908b89ce0efbc09ecbf2f_normal.png', u'following': False, u'geo_enabled': False, u'profile_banner_url': u'hddps://pbs.twimg.com/profile_banners/1219150098/1361808819', u'profile_background_image_url': u'hddp://abs.twimg.com/images/themes/theme14/bg.gif', u'screen_name': u'lodysseedohmer', u'lang': u'en', u'profile_background_tile': True, u'favourites_count': 2, u'name': u'Doh', u'notifications': False, u'url': None, u'created_at': u'Mon Feb 25 16:03:30 +0000 2013', u'contributors_enabled': False, u'time_zone': u'Eastern Time (US & Canada)', u'protected': False, u'default_profile': False, u'is_translator': False}]
答案 0 :(得分:1)
请执行以下操作:
from twython import Twython
#Auth steps
twitter = Twython(AUTH)
samplefollower = ["259784090","136953436","1219150098"]
#We create a comma separated string from the previous list
comma_separated_string = ",".join(samplefollower)
#Now, we query twitter with the comma separated list
output = twitter.lookup_user(user_id=comma_separated_string)
username_list=[]
#Loop through the results
for user in output:
username_list.append(user['screen_name'])
print username_list
该程序将显示:
[u'jwchoi13', u'djdoh88', u'lodysseedohmer']