在python中访问多个twitter用户配置文件

时间:2014-08-19 01:49:03

标签: python json twitter

因此,我尝试访问Twitter的API,以获取用户ID的详细信息,以获取我拥有的用户ID列表。 以下代码似乎适用于此,我能够获得json数据输出。我的问题是,我是否可以为多个用户(几千个用户)执行此操作,如果是这样,如何以表格格式获取它们?

以下是包含1个用户的结果输出的代码

def get_user_profile(twitter_api, screen_names=None, user_ids=None):

    # Must have either screen_name or user_id (logical xor)
    assert (screen_names != None) != (user_ids != None), \
    "Must have screen_names or user_ids, but not both"

    items_to_info = {}

    items = screen_names or user_ids

    while len(items) > 0:

        # Process 100 items at a time per the API specifications for /users/lookup.
        # See https://dev.twitter.com/docs/api/1.1/get/users/lookup for details.

        items_str = ','.join([str(item) for item in items[:100]])
        items = items[100:]

        if screen_names:
            response = make_twitter_request(twitter_api.users.lookup, 
                                            screen_name=items_str)
        else: # user_ids
            response = make_twitter_request(twitter_api.users.lookup, 
                                            user_id=items_str)

        for user_info in response:
            if screen_names:
                items_to_info[user_info['screen_name']] = user_info
            else: # user_ids
                items_to_info[user_info['id']] = user_info

    return items_to_info

response = get_user_profile(twitter_api, screen_names=["Juniorsantanab1"])

print json.dumps(response, indent=1)

输出:

{
 "Juniorsantanab1": {
  "follow_request_sent": false, 
  "profile_use_background_image": true, 
  "default_profile_image": true, 
  "id": 2565811553, 
  "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", 
  "verified": false, 
  "profile_text_color": "333333", 
  "profile_image_url_https": "https://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png", 
  "profile_sidebar_fill_color": "DDEEF6", 
  "entities": {
   "description": {.......etc

0 个答案:

没有答案