从reddit的API使用Python中PRAW解码JSON [英] Decoding JSON from Reddit API in Python using PRAW

查看:265
本文介绍了从reddit的API使用Python中PRAW解码JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PRAW在一个Python / GTK应用reddit的API。我已经成功地使用API​​,但我似乎无法能够去code中的JSON使用。应该知道,我在Python和GTK应用程序的初学者。

 # -  *  - 模式:Python的;编码:UTF-8;缩进标签模式:无;标签宽度:4  -  *  - 
### BEGIN许可
#此文件是在公共领域
### END许可进口的gettext
从进口的gettext作为的gettext _
gettext.textdomain('redditreader')从gi.repository进口的Gtk#pylint的:禁止= E0611
进口记录
记录= logging.getLogger('redditreader')从redditreader_lib导入窗口
从redditreader.AboutRedditreaderDialog进口AboutRedditreaderDialog
从redditreader。preferencesRedditreaderDialog进口preferencesRedditreaderDialog进口praw进口JSON
进口simplejson
从pprint进口pprint#见redditreader_lib.Window.py关于这个类是如何工作的详细信息
类RedditreaderWindow(窗口):
    __gtype_name__ =RedditreaderWindow    高清finish_initializing(个体经营,建设者):#pylint的:禁止= E1002
        设置主窗口中的
        超(RedditreaderWindow,个体经营).finish_initializing(制造商)        self.AboutDialog = AboutRedditreaderDialog
        自我。preferencesDialog = preferencesRedditreaderDialog        #code其他初始化动作应该在这里说。
R = praw.Reddit(USER_AGENT ='范例')
尝试:
    意见书= r.get_front_page(限值为5)
    [STR(X)在提交X]
    jsondatafirst = simplejson.loads(STR(意见书))
    jsondata = UNI code(jsondatafirst,'utf-8')
    打印(jsondata)
除了(simplejson.de coder.JSONDe codeError,ValueError错误):
    打印解码JSON已经失败


解决方案

通过PRAW你不需要做任何的JSON解码为PRAW处理所有为你。

说,例如,你要打印出upvotes的数量,downvotes的数量,以及提交标题每个提交。你可以这样做:

 提交在r.get_front_page(限值为5):
    打印submission.ups,submission.downs,submission.title

如果你想看到所有可用的对象提交您可以运行使用的属性:

 进口pprint
提交在r.get_front_page(限值为5):
    pprint.pprint商(VAR(提交))

此外,如果你想从一个提交意见,那么你可以使用 submission.comments 属性。您也可以手动看JSON响应的要求,看看有什么属性的通过PRAW(应的可用的例如)。

属性没有明确任何地方列出的对象,因为该属性是从什么键名是在请求相关的JSON响应直接创建。

I am using PRAW for Reddit API in a Python/GTK application. I have been successful in using the API, but I can't seem to be able to decode the JSON for use. It should be known that I am a beginner in Python and GTK applications.

# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# This file is in the public domain
### END LICENSE

import gettext
from gettext import gettext as _
gettext.textdomain('redditreader')

from gi.repository import Gtk # pylint: disable=E0611
import logging
logger = logging.getLogger('redditreader')

from redditreader_lib import Window
from redditreader.AboutRedditreaderDialog import AboutRedditreaderDialog
from redditreader.PreferencesRedditreaderDialog import PreferencesRedditreaderDialog

import praw

import json
import simplejson
from pprint import pprint

# See redditreader_lib.Window.py for more details about how this class works
class RedditreaderWindow(Window):
    __gtype_name__ = "RedditreaderWindow"

    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the main window"""
        super(RedditreaderWindow, self).finish_initializing(builder)

        self.AboutDialog = AboutRedditreaderDialog
        self.PreferencesDialog = PreferencesRedditreaderDialog

        # Code for other initialization actions should be added here.
r = praw.Reddit(user_agent='example')
try:
    submissions = r.get_front_page(limit=5)
    [str(x) for x in submissions]
    jsondatafirst = simplejson.loads(str(submissions))
    jsondata = unicode(jsondatafirst, 'utf-8')
    print(jsondata)
except (simplejson.decoder.JSONDecodeError, ValueError):
    print 'Decoding JSON has failed'

解决方案

With PRAW you do not need to do any json decoding as PRAW handles all of that for you.

Say for example for each submission you want to print out the number of upvotes, the number of downvotes, and the submission title. You could do:

for submission in r.get_front_page(limit=5):
    print submission.ups, submission.downs, submission.title

If you want to see all the attributes available to use on a submission object you can run:

import pprint
for submission in r.get_front_page(limit=5):
    pprint.pprint(vars(submission))

Additionally if you want to get the comments from a submission then you can use the submission.comments property. You can also manually look at the json response for a request to see what attributes should be available through PRAW (example).

The attributes are not explicitly listed anywhere for the objects because the attributes are created directly from whatever the key name is in the associated json response for the request.

这篇关于从reddit的API使用Python中PRAW解码JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆