鱼C论坛

 找回密码
 立即注册
查看: 3606|回复: 5

中国天气api为什么不能用

[复制链接]
发表于 2017-10-14 18:06:04 | 显示全部楼层 |阅读模式

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

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

x
大神帮帮忙看看,困扰好多天了就是不知道错哪了,代码如下:
import urllib.request
import json
city = {
    '北京':'101010100',
    '海淀':'101010200',
    '朝阳':'101010300',
    '顺义':'101010400',
    '怀柔':'101010500',
    '通州':'101010600',
    '延庆':'101010800',
    '石景山':'101011000',
    '大兴':'101011100',
    '房山':'101011200',
    '密云':'101011300',
    '门头沟':'101011400',
    '平谷':'101011500',
    '八达岭':'101011600',
    '佛爷顶':'101011700',
    '汤河口':'101011800',
    '密云上甸子':'101011900',
    '斋堂':'101012000',
    '霞云岭':'101012100',
    }
password = input('请输入城市:')
name1 = city[password]
File1 = urllib.request.urlopen('http://m.weather.com.cn/data/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('按任意键退出:')

你看看那个网址,为什么不能用,这个代码执行没有错误,但是一输入城市就报错,

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-10-14 18:21:08 | 显示全部楼层
网址不能用了。重新定位网址,抓包
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-15 09:21:15 | 显示全部楼层
本帖最后由 purplenight 于 2017-10-15 17:43 编辑

1. xxx,http://www.weather.com.cn/weather1d/101010100.shtml
2. JSON格式类似
  1. {
  2.     "weatherinfo": {
  3.         "city": "北京",
  4.         "cityid": "101010100",
  5.         "temp1": "-2℃",
  6.         "temp2": "16℃",
  7.         "weather": "晴",
  8.     }
  9. }
  10. # 解析上边,部分码。其它城市自行扩展
  11.     req = urllib.request.Request('http://www.weather.com.cn/data/cityinfo/101010100.html')
  12.     with urllib.request.urlopen(req) as f:
  13.         rbuf = f.read()
  14.     text = gzip.decompress(rbuf).decode()
  15.     weather = json.loads(text)

  16.     #for key, value in weather['weatherinfo'].items(): print(key, value)
  17.     print(weather['weatherinfo']['city'])
  18.     print(weather['weatherinfo']['weather'])
  19.     print(weather['weatherinfo']['temp1'])
  20.     print(weather['weatherinfo']['temp2'])
复制代码

3. Json都这个样,送个API: http://www.sojson.com/open/api/weather/json.shtml?city=%E5%8C%97%E4%BA%AC
4. Json美化: http://tool.lanrentuku.com/jsformat/

reportWeather.zip

658 Bytes, 下载次数: 10

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-15 11:46:27 | 显示全部楼层
purplenight 发表于 2017-10-15 09:21
http://www.weather.com.cn/weather1d/101010100.shtml

你好,非常感谢你的帮助,我还有一个问题就是,这个api接口,在输入这个网址后,还需要我上面写的代码city=
{北京
。。。。。。
写这个和不写这个有什么区别,
我刚刚把上面的网址换成了你的,但是还是报错,错误如下:
Traceback(最近的通话):
文件“C:/用户/ dell / AppData /地方/程序/ Python / Python36-32/5555555。py“,第28行,<模块>
weatherJSON = json.JSONDecoder().decode(weatherHTML)
戴尔用户文件“C:\ \ \ AppData \本地Python \程序\ \ Python36-32 \ lib \ json \解码器。“py”,第339行,解码
obj,结束=自我。raw_decode(年代,idx = _w(年代,0)指标())最终
戴尔用户文件“C:\ \ \ AppData \本地Python \程序\ \ Python36-32 \ lib \ json \解码器。py“,357行,在raw_decode中
从None中提高JSONDecodeError(“期望值”,s,error .value)
json.decoder。JSONDecodeError:期望值:第1行第1列(char 0)

再次恳求您的帮助,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-15 14:04:10 | 显示全部楼层
试试这个呢:
http://www.weather.com.cn/data/cityinfo/101010100.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-15 15:29:35 | 显示全部楼层
xindong 发表于 2017-10-15 14:04
试试这个呢:
http://www.weather.com.cn/data/cityinfo/101010100.html

恳请您帮我调试一下:
import urllib.request
import json


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('按任意键退出:')
为什么不对!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-24 11:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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