0

我正在使用Tumblr API —— 我想构建一个小小的 doo-dad,它将我所有的帖子拉到我的各种 Tumblr 上,并每周一次将它们归档到我的 WordPress 博客中。

因此,每次脚本运行时,我都想记录最大帖子 ID。所以我试图让这样的事情起作用:

    jdata = json.loads(rawjson)
    jposts = jdata['response']['posts']
    for post in jposts:
        print post['id'] 
    print max(jposts['id'])

我得到的是很多帖子 ID(预期), list indices must be integers, not str但如果我这样做,print type(post['id'])Python 会将它们识别为<type 'long'>. 所以......我在这里做错了什么?

这是Tumblr 的示例输出片段,您可以将其用于rawjson

{
   "meta":{
      "status":200,
      "msg":"OK"
   },
   "response":{
      "blog":{
         "title":"Scipsy",
         "name":"scipsy",
         "posts":8524,
         "url":"http:\/\/scipsy.tumblr.com\/",
         "updated":1365196814,
         "description":"\u0022Science is interesting and if you don\u0027t agree, fuck off\u0022",
         "ask":true,
         "ask_anon":true,
         "is_nsfw":false,
         "share_likes":false
      },
      "posts":[
         {
            "blog_name":"scipsy",
            "id":47218422365,
            "post_url":"http:\/\/scipsy.tumblr.com\/post\/47218422365\/you-are-missed-that-is-all",
            "slug":"you-are-missed-that-is-all",
            "type":"answer",
            "date":"2013-04-05 21:20:14 GMT",
            "timestamp":1365196814,
            "state":"published",
            "format":"html",
            "reblog_key":"slI4NU3a",
            "tags":[
               "maybe I should start another blog",
               "or maybe not",
               "Maybe I have a concussion from all the throws received during judo and I\u0027m not thinking straight",
               "yeah I think that\u0027s it"
            ],
            "short_url":"http:\/\/tmblr.co\/ZW3EPyh_R-9T",
            "highlighted":[

            ],
            "note_count":35,
            "asking_name":"psydoctor8",
            "asking_url":"http:\/\/psydoctor8.tumblr.com\/",
            "question":"you are missed. that is all.",
            "answer":"\u003Cp\u003EAlthough this break from tumblr have had some positive effects on me (I\u2019ve stopped from compulsively sifting through the NASA\u2019s archive like a maniac and I no longer fell the need to stay up all night trying to answer the most absurd questions) I have to admit that I miss all this sciency stuff and I miss a lot the awesome people I&#8217;ve known through this silly blog.\u003C\/p\u003E"
         },
         {
            "blog_name":"scipsy",
            "id":32267453988,
            "post_url":"http:\/\/scipsy.tumblr.com\/post\/32267453988\/in-the-last-months-this-blog-has-experienced-a",
            "slug":"in-the-last-months-this-blog-has-experienced-a",
            "type":"text",
            "date":"2012-09-25 16:31:00 GMT",
            "timestamp":1348590660,
            "state":"published",
            "format":"html",
            "reblog_key":"0EMwke5R",
            "tags":[

            ],
            "short_url":"http:\/\/tmblr.co\/ZW3EPyU3IaOa",
            "highlighted":[

            ],
            "note_count":246,
            "title":null,
            "body":"\u003Cp\u003EIn the last months this blog has experienced a progressive decline in the number of produced posts. There are several reasons that come to my mind to explain why is that, but probably the better one is about the fact that I lost motivation. Unexpectedly, despite the lack of regular updates the blog gained more and more followers. [That&#8217;s flattering, but at the same time makes me suspicious about the relationship between quality of a blog and number of followers.]\u00a0\u003C\/p\u003E\n\u003Cp\u003EAnyway, I always feel a little lost when I follow a blog and it slowly fades away, and then it just stops posting, without saying anything, so I thought to make this post.\u003C\/p\u003E\n\u003Cp\u003EI&#8217;m not going to update \u003Cem\u003Escipsy\u003C\/em\u003E anymore. This could change, but for now I don&#8217;t feel like posting here anymore. I&#8217;m not going to delete it.\u003C\/p\u003E\n\u003Cp\u003EIf someone would like to stay in touch, just send a message or something. This is my mail: \u003Cem\u003Edr.scipsy@gmail.com\u003C\/em\u003E\u003C\/p\u003E\n\u003Cp\u003EIf someone is wondering: &#8220;\u003Cem\u003EWho will fill my dash with science now?\u003C\/em\u003E\u0022 here&#8217;s a list of \u003Cem\u003Esciency\u003C\/em\u003E tumblr I followed:\u003C\/p\u003E\n\u003Cul\u003E\u003Cli\u003E\u003Ca href=\u0022http:\/\/psydoctor8.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Epsydoctor8\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/electricorchid.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eelectricorchid\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/gradmom.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Egradmom\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/www.itsokaytobesmart.com\/\u0022 target=\u0022_blank\u0022\u003Eitsokaytobesmart\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/realcleverscience.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Erealcleverscience\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/scientistintraining.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Escientistintraining\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/crookedindifference.com\/\u0022 target=\u0022_blank\u0022\u003Ecrookedindifference\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/ohyeahdevelopmentalbiology.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eohyeahdevelopmentalbiology\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/scienceisbeauty.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Escienceisbeauty\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/fuckyeahneuroscience.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Efuckyeahneuroscience\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/climateadaptation.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eclimateadaptation\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/exp.lore.com\/\u0022 target=\u0022_blank\u0022\u003Eexp.lore\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/xenogifh.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Exenogifh\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/www.ziyadnazem.info\/\u0022 target=\u0022_blank\u0022\u003Eziyadnazem\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/intothecontinuum.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eintothecontinuum\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/approachingsignificance.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eapproachingsignificance\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/sciencesoup.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Esciencesoup\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/blog.matthen.com\/\u0022 target=\u0022_blank\u0022\u003Ematthen\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/ulaulaman.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eulaulaman\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/mindovermatterzine.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Emindovermatterzine\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/doctorswithoutborders.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Edoctorswithoutborders\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/mothernaturenetwork.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Emothernaturenetwork\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/wnycradiolab.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Ewnycradiolab\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/blog.nysci.org\/\u0022 target=\u0022_blank\u0022\u003Enysci\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/amnhnyc.tumblr.com\u0022 target=\u0022_blank\u0022\u003Eamnhnyc\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/discoverynews.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Ediscoverynews\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/tumblr.poptech.org\/\u0022 target=\u0022_blank\u0022\u003Epoptech\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/retina.smithsonianmag.com\/\u0022 target=\u0022_blank\u0022\u003Eretina.smithsonianmag\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/onearth.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eonearth\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/huffpostscience.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Ehuffpostscience\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/bpod-mrc.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Ebpod-mrc\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/blog.tedx.com\/\u0022 target=\u0022_blank\u0022\u003Etedx\u003C\/a\u003E\u003C\/li\u003E\n\u003C\/ul\u003E\u003Cp\u003EThat&#8217;s it, I think.\u003C\/p\u003E\n\u003Cp\u003E\u003Csmall\u003E\u003Cem\u003ESo long, and thanks for all the fish.\u003C\/em\u003E\u003C\/small\u003E\u003C\/p\u003E"
         }
      ],
      "total_posts":8524
   }
}
4

2 回答 2

3

jPosts 是一个列表

jPosts = [{'id':123},{'id':54233},etc] #example 

所以你不能说jPosts['id'],因为它是一个列表而不是字典,但是你可以说 jPosts[0]['id']因为你现在在 jPost 的第 0 个索引上使用 id ,这一本字典

我想你想要

max(jPosts,key=lambda item:item['id']) #compare based on EACH items 'id' field
于 2013-09-16T19:43:42.377 回答
2

您可以将max()key指定的参数一起使用:

from operator import itemgetter
print max(jPosts, key=itemgetter('id'))['id']  # prints 47218422365
于 2013-09-16T19:47:11.163 回答