Подтвердить что ты не робот

Python 3 Получить и проанализировать API JSON

Как бы я проанализировал ответ json api с помощью python? В настоящее время у меня есть это:

import urllib.request
import json

url = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'

def response(url):
    with urllib.request.urlopen(url) as response:
        return response.read()

res = response(url)
print(json.loads(res))

Я получаю эту ошибку: TypeError: объект JSON должен быть str, а не 'bytes'

Что такое pythonic способ справиться с json apis?

4b9b3361

Ответ 1

Версия 1: (выполните pip install requests перед запуском скрипта)

import requests
r = requests.get(url='https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty')
print(r.json())

Версия 2: (перед запуском скрипта выполните pip install wget)

import wget

fs = wget.download(url='https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty')
with open(fs, 'r') as f:
    content = f.read()
print(content)

Ответ 2

вы можете использовать стандартную библиотеку python3:

import urllib.request
import json
url = 'http://www.reddit.com/r/all/top/.json'
req = urllib.request.Request(url)

##parsing response
r = urllib.request.urlopen(req).read()
cont = json.loads(r.decode('utf-8'))
counter = 0

##parcing json
for item in cont['data']['children']:
    counter += 1
    print("Title:", item['data']['title'], "\nComments:", item['data']['num_comments'])
    print("----")

##print formated
#print (json.dumps(cont, indent=4, sort_keys=True))
print("Number of titles: ", counter)

будет выглядеть следующим образом:

...
Title: Maybe we shouldn't let grandma decide things anymore.  
Comments: 2018
---- 
Title: Carrie Fisher and Her Stunt Double Sunbathing on the Set of Return of The Jedi, 1982  
Comments: 880
---- 
Title: fidget spinner  
Comments: 1537
---- 
Number of titles:  25

Ответ 3

Я обычно использовал пакет requests с пакетом json. Следующий код должен быть подходящим для ваших нужд:

import requests
import json

url = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'
r = requests.get(url)
print(json.loads(r.content))

Выход

[11008076, 
 11006915, 
 11008202,
 ...., 
 10997668,
 10999859,
 11001695]

Ответ 4

С питоном 3

import requests
import json

url = 'http://IP-Address:8088/ws/v1/cluster/scheduler'
r = requests.get(url)
data = json.loads(r.content.decode())

Ответ 5

Единственное, чего не хватает в исходном вопросе, - это вызова метода decode для объекта ответа (и даже тогда, не для каждой версии python3). Обидно, что никто не указал на это, и все вскочили на стороннюю библиотеку.

Используя только стандартную библиотеку, для простейших вариантов использования:

import json
from urllib.request import urlopen


def get(url, object_hook=None):
    with urlopen(url) as resource:  # 'with' is important to close the resource after use
        return json.load(resource, object_hook=object_hook)

Простой вариант использования:

data = get('http://url') # '{ "id": 1, "$key": 13213654 }'
print(data['id']) # 1
print(data['$key']) # 13213654

Или, если хотите, но более рискованно:

from types import SimpleNamespace

data = get('http://url', lamda o: SimpleNamespace(**o)) # '{ "id": 1, "$key": 13213654 }'
print(data.id) # 1
print(data.$key) # invalid syntax