我尝试打开为JSON,但我看到这个数组
data = requests.get('http://jsonplaceholder.typicode.com/posts/')
data = data.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))
发布于 2018-06-13 09:58:27
你的API返回list
的dict
。如果你想获得第三篇文章,你必须得到它my_post = data[2]
。
my_post
是一个dict
所以你可以得到你的帖子的主体:my_post['body']
。
https://stackoverflow.com/questions/-100005350
复制相似问题