嗨我有代码问题我尝试打开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))
答案 0 :(得分:0)
如评论所述,
您的API会返回list
dict
。如果您想获得第3次帖子,则必须使用my_post = data[2]
获取。
my_post
是dict
,因此您可以获得帖子的正文:my_post['body']
。