2014-11-01 46 views
0

我已搜索過去的小時,但找不到答案。在JSON輸出中選擇字段 - 字符串索引必須是整數,而不是str

基本上,我使用這個代碼中使用Twitter的API來提取鳴叫名單鳴叫:

from twitter import Twitter, OAuth, TwitterHTTPError 
import os 
t = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET)) 

tweets = t.lists.statuses(list_id=123456, count=5, include_rts=2) #Retrieve 5 most recent tweets 

print json.dumps(tweets, indent=4) #Prints human readable json output 

這是輸出:

[ 
    { 
     "contributors": null, 
     "truncated": false, 
     "text": "Por isso que eu te amo @Namorou \ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02 http://t.co/jbBvyvDQig", 
     "in_reply_to_status_id": null, 
     "id": 528407407794589696, 
     "favorite_count": 20, 
     "source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>", 
     "retweeted": false, 
     "coordinates": null, 
     "entities": { 
      "symbols": [], 
      "user_mentions": [ 
       { 
        "id": 267890604, 
        "indices": [ 
         23, 
         31 
        ], 
        "id_str": "267890604", 
        "screen_name": "Namorou", 
        "name": "Namorou" 
       } 
      ], 
      "hashtags": [], 
      "urls": [], 
      "media": [ 
       { 
        "expanded_url": "http://twitter.com/drogalizado/status/528407407794589696/photo/1", 
        "display_url": "pic.twitter.com/jbBvyvDQig", 
        "url": "http://t.co/jbBvyvDQig", 
        "media_url_https": "https://pbs.twimg.com/media/B1VHvE3IMAEtczX.jpg", 
        "id_str": "528407405370290177", 
        "sizes": { 
         "large": { 
          "h": 800, 
          "resize": "fit", 
          "w": 480 
         }, 
         "small": { 
          "h": 566, 
          "resize": "fit", 
          "w": 340 
         }, 
         "medium": { 
          "h": 800, 
          "resize": "fit", 
          "w": 480 
         }, 
         "thumb": { 
          "h": 150, 
          "resize": "crop", 
          "w": 150 
         } 
        }, 
        "indices": [ 
         42, 
         64 
        ], 
        "type": "photo", 
        "id": 528407405370290177, 
        "media_url": "http://pbs.twimg.com/media/B1VHvE3IMAEtczX.jpg" 
       } 
      ] 
     }, 
     "in_reply_to_screen_name": null, 
     "id_str": "528407407794589696", 
     "retweet_count": 3, 
     "in_reply_to_user_id": null, 
     "favorited": false, 
     "user": { 
      "follow_request_sent": false, 
      "profile_use_background_image": true, 
      "profile_text_color": "666666", 
      "default_profile_image": false, 
      "id": 193021875, 
      "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/524633821375782913/DylvNU_t.png", 
      "verified": false, 
      "profile_location": null, 
      "profile_image_url_https": "https://pbs.twimg.com/profile_images/491755793402699776/hVyYJkdQ_normal.png", 
      "profile_sidebar_fill_color": "252429", 
      "entities": { 
       "url": { 
        "urls": [ 
         { 
          "url": "http://t.co/yfxyixAeU5", 
          "indices": [ 
           0, 
           22 
          ], 
          "expanded_url": "http://instagram.com/oficialdrogalizado", 
          "display_url": "instagram.com/oficialdrogali\u2026" 
         } 
        ] 
       }, 
       "description": { 
        "urls": [] 
       } 
      }, 
      "followers_count": 532405, 
      "profile_sidebar_border_color": "FFFFFF", 
      "id_str": "193021875", 
      "profile_background_color": "FFFFFF", 
      "listed_count": 313, 
      "is_translation_enabled": false, 
      "utc_offset": -7200, 
      "statuses_count": 21797, 
      "description": "\u2709 PUBLICIDADE: [email protected]", 
      "friends_count": 85, 
      "location": "Porto Alegre - RS", 
      "profile_link_color": "000000", 
      "profile_image_url": "http://pbs.twimg.com/profile_images/491755793402699776/hVyYJkdQ_normal.png", 
      "following": false, 
      "geo_enabled": false, 
      "profile_banner_url": "https://pbs.twimg.com/profile_banners/193021875/1414118102", 
      "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/524633821375782913/DylvNU_t.png", 
      "name": "drogado", 
      "lang": "pt", 
      "profile_background_tile": false, 
      "favourites_count": 216, 
      "screen_name": "drogalizado", 
      "notifications": false, 
      "url": "http://t.co/yfxyixAeU5", 
      "created_at": "Mon Sep 20 19:40:24 +0000 2010", 
      "contributors_enabled": false, 
      "time_zone": "Brasilia", 
      "protected": false, 
      "default_profile": false, 
      "is_translator": false 
     }, 
     "geo": null, 
     "in_reply_to_user_id_str": null, 
     "possibly_sensitive": false, 
     "lang": "pt", 
     "created_at": "Sat Nov 01 04:45:18 +0000 2014", 
     "in_reply_to_status_id_str": null, 
     "place": null, 
     "extended_entities": { 
      "media": [ 
       { 
        "expanded_url": "http://twitter.com/drogalizado/status/528407407794589696/photo/1", 
        "display_url": "pic.twitter.com/jbBvyvDQig", 
        "url": "http://t.co/jbBvyvDQig", 
        "media_url_https": "https://pbs.twimg.com/media/B1VHvE3IMAEtczX.jpg", 
        "id_str": "528407405370290177", 
        "sizes": { 
         "large": { 
          "h": 800, 
          "resize": "fit", 
          "w": 480 
         }, 
         "small": { 
          "h": 566, 
          "resize": "fit", 
          "w": 340 
         }, 
         "medium": { 
          "h": 800, 
          "resize": "fit", 
          "w": 480 
         }, 
         "thumb": { 
          "h": 150, 
          "resize": "crop", 
          "w": 150 
         } 
        }, 
        "indices": [ 
         42, 
         64 
        ], 
        "type": "photo", 
        "id": 528407405370290177, 
        "media_url": "http://pbs.twimg.com/media/B1VHvE3IMAEtczX.jpg" 
       } 
      ] 
     } 
    }, 
    { 
     "contributors": null, 
     "truncated": false, 
     "text": "vai dormir juan uehuehueh @Namorou", 
     "in_reply_to_status_id": 528405300832116736, 
     "id": 528405454108786688, 
     "favorite_count": 7, 
     "source": "<a href=\"https://about.twitter.com/products/tweetdeck\" rel=\"nofollow\">TweetDeck</a>", 
     "retweeted": false, 
     "coordinates": null, 
     "entities": { 
      "symbols": [], 
      "user_mentions": [ 
       { 
        "id": 267890604, 
        "indices": [ 
         26, 
         34 
        ], 
        "id_str": "267890604", 
        "screen_name": "Namorou", 
        "name": "Namorou" 
       } 
      ], 
      "hashtags": [], 
      "urls": [] 
     }, 
     "in_reply_to_screen_name": "Namorou", 
     "id_str": "528405454108786688", 
     "retweet_count": 0, 
     "in_reply_to_user_id": 267890604, 
     "favorited": false, 
     "user": { 
      "follow_request_sent": false, 
      "profile_use_background_image": true, 
      "profile_text_color": "666666", 
      "default_profile_image": false, 
      "id": 193021875, 
      "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/524633821375782913/DylvNU_t.png", 
      "verified": false, 
      "profile_location": null, 
      "profile_image_url_https": "https://pbs.twimg.com/profile_images/491755793402699776/hVyYJkdQ_normal.png", 
      "profile_sidebar_fill_color": "252429", 
      "entities": { 
       "url": { 
        "urls": [ 
         { 
          "url": "http://t.co/yfxyixAeU5", 
          "indices": [ 
           0, 
           22 
          ], 
          "expanded_url": "http://instagram.com/oficialdrogalizado", 
          "display_url": "instagram.com/oficialdrogali\u2026" 
         } 
        ] 
       }, 
       "description": { 
        "urls": [] 
       } 
      }, 
      "followers_count": 532405, 
      "profile_sidebar_border_color": "FFFFFF", 
      "id_str": "193021875", 
      "profile_background_color": "FFFFFF", 
      "listed_count": 313, 
      "is_translation_enabled": false, 
      "utc_offset": -7200, 
      "statuses_count": 21797, 
      "description": "\u2709 PUBLICIDADE: [email protected]", 
      "friends_count": 85, 
      "location": "Porto Alegre - RS", 
      "profile_link_color": "000000", 
      "profile_image_url": "http://pbs.twimg.com/profile_images/491755793402699776/hVyYJkdQ_normal.png", 
      "following": false, 
      "geo_enabled": false, 
      "profile_banner_url": "https://pbs.twimg.com/profile_banners/193021875/1414118102", 
      "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/524633821375782913/DylvNU_t.png", 
      "name": "drogado", 
      "lang": "pt", 
      "profile_background_tile": false, 
      "favourites_count": 216, 
      "screen_name": "drogalizado", 
      "notifications": false, 
      "url": "http://t.co/yfxyixAeU5", 
      "created_at": "Mon Sep 20 19:40:24 +0000 2010", 
      "contributors_enabled": false, 
      "time_zone": "Brasilia", 
      "protected": false, 
      "default_profile": false, 
      "is_translator": false 
     }, 
     "geo": null, 
     "in_reply_to_user_id_str": "267890604", 
     "lang": "in", 
     "created_at": "Sat Nov 01 04:37:32 +0000 2014", 
     "in_reply_to_status_id_str": "528405300832116736", 
     "place": null 
    }, 
    { 
     "contributors": null, 
     "truncated": false, 
     "text": "mulher nenhuma presta, tudo puta.. vo vira gay.", 
     "in_reply_to_status_id": null, 
     "id": 528404407621517312, 
     "favorite_count": 62, 
     "source": "<a href=\"https://about.twitter.com/products/tweetdeck\" rel=\"nofollow\">TweetDeck</a>", 
     "retweeted": false, 
     "coordinates": null, 
     "entities": { 
      "symbols": [], 
      "user_mentions": [], 
      "hashtags": [], 
      "urls": [] 
     }, 
     "in_reply_to_screen_name": null, 
     "id_str": "528404407621517312", 
     "retweet_count": 17, 
     "in_reply_to_user_id": null, 
     "favorited": false, 
     "user": { 
      "follow_request_sent": false, 
      "profile_use_background_image": true, 
      "profile_text_color": "666666", 
      "default_profile_image": false, 
      "id": 193021875, 
      "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/524633821375782913/DylvNU_t.png", 
      "verified": false, 
      "profile_location": null, 
      "profile_image_url_https": "https://pbs.twimg.com/profile_images/491755793402699776/hVyYJkdQ_normal.png", 
      "profile_sidebar_fill_color": "252429", 
      "entities": { 
       "url": { 
        "urls": [ 
         { 
          "url": "http://t.co/yfxyixAeU5", 
          "indices": [ 
           0, 
           22 
          ], 
          "expanded_url": "http://instagram.com/oficialdrogalizado", 
          "display_url": "instagram.com/oficialdrogali\u2026" 
         } 
        ] 
       }, 
       "description": { 
        "urls": [] 
       } 
      }, 
      "followers_count": 532405, 
      "profile_sidebar_border_color": "FFFFFF", 
      "id_str": "193021875", 
      "profile_background_color": "FFFFFF", 
      "listed_count": 313, 
      "is_translation_enabled": false, 
      "utc_offset": -7200, 
      "statuses_count": 21797, 
      "description": "\u2709 PUBLICIDADE: [email protected]", 
      "friends_count": 85, 
      "location": "Porto Alegre - RS", 
      "profile_link_color": "000000", 
      "profile_image_url": "http://pbs.twimg.com/profile_images/491755793402699776/hVyYJkdQ_normal.png", 
      "following": false, 
      "geo_enabled": false, 
      "profile_banner_url": "https://pbs.twimg.com/profile_banners/193021875/1414118102", 
      "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/524633821375782913/DylvNU_t.png", 
      "name": "drogado", 
      "lang": "pt", 
      "profile_background_tile": false, 
      "favourites_count": 216, 
      "screen_name": "drogalizado", 
      "notifications": false, 
      "url": "http://t.co/yfxyixAeU5", 
      "created_at": "Mon Sep 20 19:40:24 +0000 2010", 
      "contributors_enabled": false, 
      "time_zone": "Brasilia", 
      "protected": false, 
      "default_profile": false, 
      "is_translator": false 
     }, 
     "geo": null, 
     "in_reply_to_user_id_str": null, 
     "lang": "pt", 
     "created_at": "Sat Nov 01 04:33:22 +0000 2014", 
     "in_reply_to_status_id_str": null, 
     "place": null 
    }, 
    { 
     "contributors": null, 
     "truncated": false, 
     "text": "Linkin Park - Given Up \u2764\ufe0f", 
     "in_reply_to_status_id": null, 
     "id": 528401445595652096, 
     "favorite_count": 71, 
     "source": "<a href=\"https://about.twitter.com/products/tweetdeck\" rel=\"nofollow\">TweetDeck</a>", 
     "retweeted": false, 
     "coordinates": null, 
     "entities": { 
      "symbols": [], 
      "user_mentions": [], 
      "hashtags": [], 
      "urls": [] 
     }, 
     "in_reply_to_screen_name": null, 
     "id_str": "528401445595652096", 
     "retweet_count": 34, 
     "in_reply_to_user_id": null, 
     "favorited": false, 
     "user": { 
      "follow_request_sent": false, 
      "profile_use_background_image": true, 
      "profile_text_color": "666666", 
      "default_profile_image": false, 
      "id": 193021875, 
      "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/524633821375782913/DylvNU_t.png", 
      "verified": false, 
      "profile_location": null, 
      "profile_image_url_https": "https://pbs.twimg.com/profile_images/491755793402699776/hVyYJkdQ_normal.png", 
      "profile_sidebar_fill_color": "252429", 
      "entities": { 
       "url": { 
        "urls": [ 
         { 
          "url": "http://t.co/yfxyixAeU5", 
          "indices": [ 
           0, 
           22 
          ], 
          "expanded_url": "http://instagram.com/oficialdrogalizado", 
          "display_url": "instagram.com/oficialdrogali\u2026" 
         } 
        ] 
       }, 
       "description": { 
        "urls": [] 
       } 
      }, 
      "followers_count": 532405, 
      "profile_sidebar_border_color": "FFFFFF", 
      "id_str": "193021875", 
      "profile_background_color": "FFFFFF", 
      "listed_count": 313, 
      "is_translation_enabled": false, 
      "utc_offset": -7200, 
      "statuses_count": 21797, 
      "description": "\u2709 PUBLICIDADE: [email protected]", 
      "friends_count": 85, 
      "location": "Porto Alegre - RS", 
      "profile_link_color": "000000", 
      "profile_image_url": "http://pbs.twimg.com/profile_images/491755793402699776/hVyYJkdQ_normal.png", 
      "following": false, 
      "geo_enabled": false, 
      "profile_banner_url": "https://pbs.twimg.com/profile_banners/193021875/1414118102", 
      "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/524633821375782913/DylvNU_t.png", 
      "name": "drogado", 
      "lang": "pt", 
      "profile_background_tile": false, 
      "favourites_count": 216, 
      "screen_name": "drogalizado", 
      "notifications": false, 
      "url": "http://t.co/yfxyixAeU5", 
      "created_at": "Mon Sep 20 19:40:24 +0000 2010", 
      "contributors_enabled": false, 
      "time_zone": "Brasilia", 
      "protected": false, 
      "default_profile": false, 
      "is_translator": false 
     }, 
     "geo": null, 
     "in_reply_to_user_id_str": null, 
     "lang": "en", 
     "created_at": "Sat Nov 01 04:21:36 +0000 2014", 
     "in_reply_to_status_id_str": null, 
     "place": null 
    } 
] 

我要選擇的id_strretweet_count全部5條推文輸出。 print(myjson[0]['text'])返回TypeError: string indices must be integers, not str

如何從這種類型的json結構中選擇字段?

回答

0

變量tweets是一個Python列表,所以tweets [0]是第一個。列表中的每條推文都是一個Python字典,因此您可以說推文[0] ['contributors']來獲得第一個字典的「貢獻者」值(在這種情況下,它將由Python無)。

您可以同樣瀏覽到結構的其他部分。這應該讓你的第一個鳴叫的第一個用戶提到的「ID_STR」值:

tweets[0]['entities']['user_mentions'][0]['id_str'] 
+0

嗨,吉姆,感謝您的回答,但'IndexError:列表索引超出range'的返回'IndexError:列表索引超出range'的。 – 2014-11-01 05:25:12

+0

Vince,上面的代碼片段在推文根本沒有user_mentions時不起作用,就像上面示例中的情侶一樣。你需要在這裏自己弄清細節(假設這是作業)。 – 2014-11-01 05:44:46

+0

我看到了,我用'print tweets [0] ['retweet_count']'得到了它,如果我改變'[0]'我無法獲得其他推文的retweet_count。謝謝! – 2014-11-01 05:51:17

相關問題