2012-01-16 17 views
1

所以我調用Twitter的API的最大數量:Twitter的API的Python - 對象

openurl = urllib.urlopen("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&contributor_details&include_rts=true&screen_name="+user+"&count=3600") 

,並返回一些長的文件,如:

[{"entities":{"hashtags":[],"user_mentions":[],"urls":[{"url":"http:\/\/t.co\/Hd1ubDVX","indices":[115,135],"display_url":"amzn.to\/tPSKgf","expanded_url":"http:\/\/amzn.to\/tPSKgf"}]},"coordinates":null,"truncated":false,"place":null,"geo":null,"in_reply_to_user_id":null,"retweet_count":2,"favorited":false,"in_reply_to_status_id_str":null,"user":{"contributors_enabled":false,"lang":"en","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/151701304\/theme14.gif","favourites_count":0,"profile_text_color":"333333","protected":false,"location":"North America","is_translator":false,"profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/151701304\/theme14.gif","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1642783876\/idB005XNC8Z4_normal.png","name":"User Interface Books","profile_link_color":"009999","url":"http:\/\/twitter.com\/ReleasedBooks\/genres","utc_offset":-28800,"description":"All new user interface and graphic design book releases posted on their publication day","listed_count":11,"profile_background_color":"131516","statuses_count":1189,"following":false,"profile_background_tile":true,"followers_count":732,"profile_image_url":"http:\/\/a2.twimg.com\/profile_images\/1642783876\/idB005XNC8Z4_normal.png","default_profile":false,"geo_enabled":false,"created_at":"Mon Sep 20 21:28:15 +0000 2010","profile_sidebar_fill_color":"efefef","show_all_inline_media":false,"follow_request_sent":false,"notifications":false,"friends_count":1,"profile_sidebar_border_color":"eeeeee","screen_name":"User","id_str":"193056806","verified":false,"id":193056806,"default_profile_image":false,"profile_use_background_image":true,"time_zone":"Pacific Time (US & Canada)"},"possibly_sensitive":false,"in_reply_to_screen_name":null,"created_at":"Thu Nov 17 00:01:45 +0000 2011","in_reply_to_user_id_str":null,"retweeted":false,"source":"\u003Ca href=\"http:\/\/twitter.com\/ReleasedBooks\/genres\" rel=\"nofollow\"\u003EBook Releases\u003C\/a\u003E","id_str":"136957158075011072","in_reply_to_status_id":null,"id":136957158075011072,"contributors":null,"text":"Digital Media: Technological and Social Challenges of the Interactive World - by William Aspray - Scarecrow Press. http:\/\/t.co\/Hd1ubDVX"},{"entities":{"hashtags":[],"user_mentions":[],"urls":[{"url":"http:\/\/t.co\/GMCzTija","indices":[119,139],"display_u 

好, 不同的對象被切成表和字典,我想提取不同的部分,但要做到這一點,我必須知道文件有多少個對象:

示例:

[{1:info , 2:info}][{1:info , 2:info}][{1:info , 2:info}][{1:info , 2:info}] 

所以要提取1信息的第一個表我想:

[0]['1'] 
>>>>info 

但是從最後一個對象,我需要知道有多少對象的表有表中提取它。

這是我的代碼如下所示:

table_timeline = json.loads(twitter_timeline) 

    table_timeline_inner = table_timeline[x] 
    lines = 0 
    while lines < linesmax: 
     in_reply_to_user_id = table_timeline_inner['in_reply_to_status_id_str'] 
     lines += 1 

那麼,如何找到最後的對象在此表中的值?

感謝

回答

1

我不能完全肯定這是你要找的東西,但要獲得在Python列表中的最後一個項目,使用索引-1。例如,

>>> alist = [{'position': 'first'}, {'position': 'second'}, {'position': 'third'}] 
>>> print alist[-1]['position'] 
{'position': 'third'} 
>>> print alist[-1]['position'] 
third