2

i was trying to use a code writing in python 2.7 with python 3.5,but i coudn't solve this error

TypeError: the JSON object must be str, not 'bytes'

        while has_next_page:
        after = '' if after is '' else "&after={}".format(after)
        base_url = base + node + parameters + after + since + until

        url = getFacebookPageFeedUrl(base_url)
        statuses = json.loads(request_until_succeed(url))
        reactions = getReactionsForStatuses(base_url)

        reactions = getReactionsForStatuses(base_url)

        for status in statuses['data']:

            # Ensure it is a status with the expected metadata
            if 'reactions' in status:
                status_data = processFacebookPageFeedStatus(status)
                reactions_data = reactions[status_data[0]]

                # calculate thankful/pride through algebra
                num_special = status_data[6] - sum(reactions_data)
                w.writerow(status_data + reactions_data + (num_special,))

            num_processed += 1
            if num_processed % 100 == 0:
                print(("{} Statuses Processed: {}".format
                      (num_processed, datetime.datetime.now())))

        # if there is no next page, we're done.
        if 'paging' in statuses:
            after = statuses['paging']['cursors']['after']
        else:
            has_next_page = False

the probleme is 6th line with json.load, is any one who have idea how to solve it ? thank you

here is the request_until_succeed function:

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

        print("Error for URL {}: {}".format(url, datetime.datetime.now()))
        print("Retrying.")

return response.read()
yok
  • 25
  • 7
  • 1
    Please always provide the full traceback of errors, tell us what you have already tried, and reduce the problem to a minimal example. From the error message, it looks like you need to `.decode()` the argument of `json.loads()`. – Sven Marnach Sep 11 '17 at 14:29
  • i tried decode but i got this error message: TypeError: the JSON object must be str, not 'StreamReader' – yok Sep 11 '17 at 14:42
  • Try replacing: `statuses = json.loads(request_until_succeed(url))` with `statuses = json.loads(b''.join(request_until_succeed(url)).decode())` – Yaroslav Surzhikov Sep 11 '17 at 14:53
  • i already tried this response = urlopen(url) string = response.read().decode("utf-8") statuses = json.loads(string) – yok Sep 11 '17 at 15:02
  • Check out [Dive Into Python 3's discussion of this](http://www.diveintopython3.net/http-web-services.html#dont-try-this-at-home). Also [this SO thread](https://stackoverflow.com/a/6862922/3670871). – Engineero Sep 11 '17 at 15:04
  • yaroslave i tried but got this error TypeError: sequence item 0: expected a bytes-like object, int found – yok Sep 11 '17 at 15:08
  • I AM REALLY WONDERING WHY THE CODE WORK ON PYTHON 2.7 AND DON'T WORK ON PYTHON 3.5 ???? PLEASE CAN SOME ONE GIVE SOME EXPLANATION....... THANK YOU – yok Sep 12 '17 at 08:02

0 Answers0