Python 3 Get and parse JSON API

前端 未结 5 1171
醉梦人生
醉梦人生 2021-01-31 14:58

How would I parse a json api response with python? I currently have this:

import urllib.request
import json

url = \'https://hacker-news.firebaseio.com/v0/topsto         


        
5条回答
  •  既然无缘
    2021-01-31 15:17

    I would usually use the requests package with the json package. The following code should be suitable for your needs:

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

    Output

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

提交回复
热议问题