使用PRAW从Python中解码Reddit API中的JSON

时间:2022-10-17 11:01:08

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.

我在Python / GTK应用程序中使用PRAW for Reddit API。我已成功使用API​​,但我似乎无法解码JSON以供使用。应该知道我是Python和GTK应用程序的初学者。

# -*- 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'

2 个解决方案

#1


3  

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

使用PRAW,您不需要进行任何json解码,因为PRAW会为您处理所有这些。

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:

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

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).

此外,如果您想从提交中获取评论,则可以使用submission.comments属性。您还可以手动查看请求的json响应,以查看PRAW可以使用哪些属性(示例)。

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.

未在对象的任何位置明确列出属性,因为属性是直接从关键名称在请求的关联json响应中创建的。

#2


2  

JSON is simply a dictionary of dictionaries, extended with lists, if needed.

JSON只是一个字典字典,如果需要,可以使用列表进行扩展。

A good way to get familiar with whatever JSON you're dealing with at the moment is to load it, and play around with it by accessing the dictionary elements in a more straightforward way.

熟悉当前正在处理的任何JSON的一个好方法是加载它,并通过以更直接的方式访问字典元素来解决它。

>>> import urllib2
>>> import json
>>> response = urllib2.urlopen('http://reddit.com/user/droogans.json').read()
>>> js = json.loads(response)
>>> comment = js['data']['children'][0]['data']
>>> #this is my most recent comment, along with a lot of other interesting stuff
>>> print comment['ups']
9001

So, explore the data, and you'll understand it better.

因此,探索数据,你会更好地理解它。

#1


3  

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

使用PRAW,您不需要进行任何json解码,因为PRAW会为您处理所有这些。

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:

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

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).

此外,如果您想从提交中获取评论,则可以使用submission.comments属性。您还可以手动查看请求的json响应,以查看PRAW可以使用哪些属性(示例)。

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.

未在对象的任何位置明确列出属性,因为属性是直接从关键名称在请求的关联json响应中创建的。

#2


2  

JSON is simply a dictionary of dictionaries, extended with lists, if needed.

JSON只是一个字典字典,如果需要,可以使用列表进行扩展。

A good way to get familiar with whatever JSON you're dealing with at the moment is to load it, and play around with it by accessing the dictionary elements in a more straightforward way.

熟悉当前正在处理的任何JSON的一个好方法是加载它,并通过以更直接的方式访问字典元素来解决它。

>>> import urllib2
>>> import json
>>> response = urllib2.urlopen('http://reddit.com/user/droogans.json').read()
>>> js = json.loads(response)
>>> comment = js['data']['children'][0]['data']
>>> #this is my most recent comment, along with a lot of other interesting stuff
>>> print comment['ups']
9001

So, explore the data, and you'll understand it better.

因此,探索数据,你会更好地理解它。