从 facebook 下载的页面帖子。如何处理 JSON 数据?

Downloaded page posts from facebook. How do I handle the JSON data?

我一直在关注 this tutorial 从 fb 下载一些页面信息。

我使用的是 Python 3.5,教程使用的是 python2。

一开始我遇到了 HTTP 错误代码 400 的问题,基本上是说我必须使用 https 协议而不是 http。因此,现在我在闲置中进行了测试,因为数据即将到来,它看起来像我这样的新手 JSON。但是当我尝试将它传递给 json.loads 时,它给出了这个错误

Traceback (most recent call last):
  File "C:\Users\Levo\Desktop\facebookscrape.py", line 38, in <module>
    testFacebookPageData(page_id, access_token)
  File "C:\Users\Levo\Desktop\facebookscrape.py", line 34, in testFacebookPageData
    data = json.loads(requests_until_succeed(url))
  File "C:\Users\Levo\AppData\Local\Programs\Python\Python35\lib\json\__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'

除了使用 urllib 库而不是 urllib2 库和使用 https 而不是 http 我不知道我做错了什么? https的加密有问题吗??

def requests_until_succeed(url):
    req = urllib.request.Request(url)
    success = False
    while success is False:
        try:
            response = urllib.request.urlopen(req)
            if response.getcode() == 200:
                success = True
        except Exception as e:
            print(e)
            time.sleep(5)

            print ("Error for URL %s: %s" % (url, datetime.datetime.now()))

        return response.read()


def testFacebookPageData(page_id, access_token):
    base = "https://graph.facebook.com/v2.6"
    node = "/" + page_id + "/feed"
    parameters = "/?access_token=%s" % access_token
    url = base + node + parameters

    data = json.loads(requests_until_succeed(url))

    print(json.dumps(data, indent = 4, sort_keys=True))

testFacebookPageData(page_id, access_token)

json.loads accepts python3 string, which is unicode, and responce.read() returns binary string.

使用 data = json.loads(requests_until_succeed(url).decode('utf-8')) 因为响应很可能是 utf-8。