2

So, I'm retrieving a blog post from Tumblr using the pytumblr API. I want to retrieve a post and extract only the post content. Technically Tumblr sends it to me in a dict, but the format is very, very confusing. In addition to that, it uses both single and double quotes! Here's my code:

post = client.posts(blogName, type = 'text', tag = 'suggestion', limit = 1)
postformat = str(post[u'posts']).replace("[", "").replace("]", "")
blog = dict(ast.literal_eval(postformat))
print(post[u'body']).replace("<p>", "").replace("</p>", "")

First, Tumblr gives me a very large dictionary with only 3 keys, but each of them has a dictionary within it! (???). So, I need to take the first key that I'm looking for, and then turn the contents of that key back into a dict using ast. When I tried that, I got a different error. So, I deleted the brackets around post and used ast to try and interpret the content. But when I do, t throws a SyntaxError on line 3. Here's post[u'posts'] in its raw, very confusing format. But since stackoverflow puts it on one line, I also put it on pastebin at http://pastebin.com/9fnuS2F6

{u'body': u'<p>Concept: we&rsquo;re in a cute little cottage, surrounded by flowers. I&rsquo;m making breakfast and you&rsquo;re on your way home from walking the dogs. We keep our own bees and collect honey from them; they are as happy and safe as we are.</p>', u'liked': False, u'followed': False, u'reblog_key': u'tlPNsk6e', u'reblog': {u'comment': u'<p>Concept: we\u2019re in a cute little cottage, surrounded by flowers. I\u2019m making breakfast and you\u2019re on your way home from walking the dogs. We keep our own bees and collect honey from them; they are as happy and safe as we are.</p>', u'tree_html': u''}, u'can_send_in_message': True, u'id': 146647556007L, u'post_url': u'http://affectionsuggestion.tumblr.com/post/146647556007/concept-were-in-a-cute-little-cottage', u'can_reply': True, u'title': None, u'tags': u'queued', u'suggestion', u'suggestion blog', u'concept', u'bees', u'love', u'future', u'happy', u'happiness', u'couple', u'relationship', u'nature', u'lesbians', u'cute', u'highlighted': , u'recommended_source': None, u'state': u'published', u'short_url': u'https://tmblr.co/ZzO-5i28au1_d', u'type': u'text', u'recommended_color': None, u'format': u'html', u'timestamp': 1467190899, u'note_count': 523, u'trail': {u'content': u'<p>Concept: we\u2019re in a cute little cottage, surrounded by flowers. I\u2019m making breakfast and you\u2019re on your way home from walking the dogs. We keep our own bees and collect honey from them; they are as happy and safe as we are.</p>', u'content_raw': u'<p>Concept: we\u2019re in a cute little cottage, surrounded by flowers. I\u2019m making breakfast and you\u2019re on your way home from walking the dogs. We keep our own bees and collect honey from them; they are as happy and safe as we are.</p>', u'is_current_item': True, u'blog': {u'active': True, u'theme': {u'title_font_weight': u'bold', u'title_color': u'#444444', u'header_bounds': u'', u'title_font': u'Gibson', u'link_color': u'#3D7291', u'header_image_focused': u'https://secure.assets.tumblr.com/images/default_header/optica_pattern_05.png?_v=671444c5f47705cce40d8aefd23df3b1', u'show_description': True, u'show_header_image': False, u'header_stretch': True, u'body_font': u'Helvetica Neue', u'show_title': True, u'header_image_scaled': u'https://secure.assets.tumblr.com/images/default_header/optica_pattern_05.png?_v=671444c5f47705cce40d8aefd23df3b1', u'avatar_shape': u'square', u'show_avatar': False, u'background_color': u'#FEB0B0', u'header_image': u'https://secure.assets.tumblr.com/images/default_header/optica_pattern_05.png?_v=671444c5f47705cce40d8aefd23df3b1'}, u'share_following': False, u'name': u'affectionsuggestion', u'share_likes': False}, u'is_root_item': True, u'post': {u'id': u'146647556007'}}, u'date': u'2016-06-29 09:01:39 GMT', u'slug': u'concept-were-in-a-cute-little-cottage', u'blog_name': u'affectionsuggestion', u'summary': u"Concept: we're in a cute little cottage, surrounded by flowers. I'm making breakfast and you're on your way home from walking..."}
4

1 回答 1

1

语法错误是由您删除括号引起的。在字典中的某处有一个标签列表,而您拥有的字符串是:

... u'title': None, u'tags': u'queued', u'suggestion', u'suggestion blog', ...

也就是说,它从字典表示法切换到列表表示法,因为[]标签周围没有了。

我怀疑post['posts']只是一个帖子列表,post['posts'][0]是第一个,并且post['posts'][0]['body']是您正在寻找的正文。

于 2016-06-30T14:56:18.500 回答