|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import urllib.request
- import json
- import pickle
- #建立城市字典
- pickle_file = open('citycount.pkl', 'rb')
- city = pickle.load(pickle_file)
- password=input('请输入城市:')
- name1=city[password]
- File1 =urllib.request.urlopen('http://m.weather.com.cn/mweather/'+name1+'.shtml')#打开url
- weatherHTML = File1.read().decode('utf-8')#读入打开的url
- weatherJSON = json.JSONDecoder().decode(weatherHTML)#创建json
- weatherInfo = weatherJSON['weatherinfo']
- #打印信息
- print ( '城市:', weatherInfo['city'])
- print ('时间:', weatherInfo['date_y'])
- print ( '24小时天气:')
- print ('温度:', weatherInfo['temp1'])
- print ('天气:', weatherInfo['weather1'])
- print ('风速:', weatherInfo['wind1'])
- print ('紫外线:', weatherInfo['index_uv'])
- print ('穿衣指数:', weatherInfo['index_d'])
- print ('48小时天气:')
- print ('温度:', weatherInfo['temp2'])
- print ('天气:', weatherInfo['weather2'])
- print ('风速:', weatherInfo['wind2'])
- print ('紫外线:', weatherInfo['index48_uv'])
- print ('穿衣指数:', weatherInfo['index48_d'])
- print ('72小时天气:')
- print ('温度:', weatherInfo['temp3'])
- print ('天气:', weatherInfo['weather3'])
- print ('风速:', weatherInfo['wind3'])
- input ('按任意键退出:')
复制代码
这个代码一直报错,不知道为什么?求大佬解答
第9句爬取返回的是html,不是json,传给json处理不是json的数据就会报这个错。
|
|