Python 3 Get and parse JSON API

前端 未结 5 1175
醉梦人生
醉梦人生 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:12

    Version 1: (do a pip install requests before running the script)

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

    Version 2: (do a pip install wget before running the script)

    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)
    

提交回复
热议问题