鱼C论坛

 找回密码
 立即注册
查看: 2105|回复: 9

对象不是可订阅是什么意思

[复制链接]
发表于 2017-10-15 15:38:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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/cityinfo/101010100.html')
weatherHTML = File1.read().decode('utf-8')
weatherJSON = json.JSONDecoder().decode(weatherHTML)
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('按任意键退出:')

请大神不吝赐教!谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-10-15 17:42:25 | 显示全部楼层
weatherInfo 返回的值里面没有你列出的那么多项。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-15 17:45:09 | 显示全部楼层
xindong 发表于 2017-10-15 17:42
weatherInfo 返回的值里面没有你列出的那么多项。

麻烦说的再明白点,我搞不明白,,,你把我的代码调试一下,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-15 18:03:59 | 显示全部楼层
你试试这段代码,我是可以运行的

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Oct 15 17:34:58 2017


  4. """

  5. import urllib.request
  6. import json
  7. import pickle

  8. pickle_file = open('city_data.pkl','rb')
  9. city = pickle.load(pickle_file)

  10. password = input('请输入城市:')
  11. name1 = city[password]
  12. File1 = urllib.request.urlopen('http://www.weather.com.cn/data/sk/101010100.html')
  13. weatherHTML = File1.read().decode('utf-8')
  14. weatherJSON = json.JSONDecoder().decode(weatherHTML)
  15. weatherInfo = weatherJSON['weatherinfo']

  16. File1 = urllib.request.urlopen('http://www.weather.com.cn/data/cityinfo/101010100.html')
  17. weatherHTML = File1.read().decode('utf-8')
  18. weatherJSON = json.JSONDecoder().decode(weatherHTML)
  19. weatherInfo2 = weatherJSON['weatherinfo']


  20. print('城市:',weatherInfo['city'])
  21. print('时间:',weatherInfo['time'])
  22. print('温度:',weatherInfo['temp'])
  23. print('天气:',weatherInfo2['weather'])
  24. print('风向:',weatherInfo['WD'])
  25. print('风速:',weatherInfo['WS'])


  26. input('按任意键退出:')
复制代码

结果:

请输入城市:北京
城市: 北京
时间: 17:05
温度: 18
天气: 晴
风向: 东南风
风速: 1级

按任意键退出:


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-15 18:27:53 | 显示全部楼层
xindong 发表于 2017-10-15 18:03
你试试这段代码,我是可以运行的

我刚刚试过了,
执行错误,Traceback (most recent call last):
  File "C:/Users/dell/Desktop/886.py", line 12, in <module>
    pickle_file = open('city_data.pkl','rb')
FileNotFoundError: [Errno 2] No such file or directory: 'city_data.pkl'

但是我刚刚保存了‘city_data.pkl’我可以找到这个文件,

但错误是没有这个文件,,

是不是和你那个网址和我列出的必须一致?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-15 18:28:56 | 显示全部楼层
xindong 发表于 2017-10-15 18:03
你试试这段代码,我是可以运行的

你这个网址打开时乱码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-15 18:36:30 | 显示全部楼层
你的city_data.pkl和你的.py文件是在一个目录下吗?

你是什么操作系统,用的是什么python软件?

Image 18.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-16 12:45:02 | 显示全部楼层
xindong 发表于 2017-10-15 18:36
你的city_data.pkl和你的.py文件是在一个目录下吗?

你是什么操作系统,用的是什么python软件?

对啊,在python目录下,python3.6(32bit)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-16 14:03:11 | 显示全部楼层
你看我的截图,你的运行起来是什么样的?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-18 12:56:55 | 显示全部楼层
枫丹白露666 发表于 2017-10-16 12:45
对啊,在python目录下,python3.6(32bit)

window,不是你的这个
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 01:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表