你试试这段代码,我是可以运行的
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 15 17:34:58 2017
"""
import urllib.request
import json
import pickle
pickle_file = open('city_data.pkl','rb')
city = pickle.load(pickle_file)
password = input('请输入城市:')
name1 = city[password]
File1 = urllib.request.urlopen('http://www.weather.com.cn/data/sk/101010100.html')
weatherHTML = File1.read().decode('utf-8')
weatherJSON = json.JSONDecoder().decode(weatherHTML)
weatherInfo = weatherJSON['weatherinfo']
File1 = urllib.request.urlopen('http://www.weather.com.cn/data/cityinfo/101010100.html')
weatherHTML = File1.read().decode('utf-8')
weatherJSON = json.JSONDecoder().decode(weatherHTML)
weatherInfo2 = weatherJSON['weatherinfo']
print('城市:',weatherInfo['city'])
print('时间:',weatherInfo['time'])
print('温度:',weatherInfo['temp'])
print('天气:',weatherInfo2['weather'])
print('风向:',weatherInfo['WD'])
print('风速:',weatherInfo['WS'])
input('按任意键退出:')
结果:
请输入城市:北京
城市: 北京
时间: 17:05
温度: 18
天气: 晴
风向: 东南风
风速: 1级
按任意键退出:
|