Решения кода Python запрос свободы - PullRequest
0 голосов
/ 05 июня 2018

привет, у меня проблема с кодом, я пытаюсь открыть как json, но я вижу это как массив

data = requests.get('http://jsonplaceholder.typicode.com/posts/')
data = data.json()

Я новичок в языке Python и впервые работаю с json, это код

import requests


# Complete the function get_post_body so that it returns the content of the post o
# given idnplaceholder.typicode.com/posts/

    def get_post_body (post_id):
        return data ['body']

# Complete the get_commenters_data function in such a way that
# returned a list of tuples containing the name and surname of the commentator
# and its email address
# URL: http://jsonplaceholder.typicode.com/posts/<post_id>/comments

    def get_commenters_data (post_id):
        pass

# Complete the function get_user_posts in such a way that it returns a list
# topics of user posts about given id
# URL: http://jsonplaceholder.typicode.com/posts?userId= <user_id>

    def get_user_posts (user_id):
        pass

    if __name__ == '__main__':
        print (get_post_body (10))
        print ('=' * 20)
        print (get_commenters_data (10))
        print ('=' * 20)
        print (get_user_posts (1))

1 Ответ

0 голосов
/ 06 июня 2018

Как сказано в комментариях,

Ваш API возвращает list из dict.Если вы хотите получить 3-й пост, вам придется получить его с my_post = data[2].

my_post - это dict, поэтому вы можете получить текст своего поста: my_post['body'].

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...