Discuss / Python / 交作业

交作业

Topic source

-- coding: utf-8 --

from urllib import request import json

def fetch_data(url): with request.urlopen(url) as f: urldata = f.read().decode('utf-8') return json.loads(urldata) # 将一个json的对象反序列化为Python object?

URL = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20%3D%202151330&format=json' data = fetch_data(URL) print(data) assert data['query']['results']['channel']['location']['city'] == 'Beijing' print('ok')


  • 1

Reply