首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >解决方案代码Python请求libery

解决方案代码Python请求libery
EN

Stack Overflow用户
提问于 2018-06-13 00:54:57
回答 1查看 0关注 0票数 0

我尝试打开为JSON,但我看到这个数组

代码语言:javascript
复制
data = requests.get('http://jsonplaceholder.typicode.com/posts/')
data = data.json()

这是代码

代码语言:javascript
复制
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))
EN

回答 1

Stack Overflow用户

发布于 2018-06-13 09:58:27

你的API返回listdict。如果你想获得第三篇文章,你必须得到它my_post = data[2]

my_post是一个dict所以你可以得到你的帖子的主体:my_post['body']

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档