鱼C论坛

 找回密码
 立即注册
查看: 2872|回复: 4

查询天气报错

[复制链接]
发表于 2017-9-11 17:25:22 | 显示全部楼层 |阅读模式

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

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

x

报错信息:
Traceback (most recent call last):
  File "F:/Python/Py_Job/test1/cs.py", line 15, in <module>
    weatherHTML = urllib.request.urlopen(File1).read()#读入打开的url
  File "F:\Python\PyAnzhuang\lib\urllib\request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "F:\Python\PyAnzhuang\lib\urllib\request.py", line 461, in open
    response = meth(req, response)
  File "F:\Python\PyAnzhuang\lib\urllib\request.py", line 571, in http_response
    'http', request, response, code, msg, hdrs)
  File "F:\Python\PyAnzhuang\lib\urllib\request.py", line 499, in error
    return self._call_chain(*args)
  File "F:\Python\PyAnzhuang\lib\urllib\request.py", line 433, in _call_chain
    result = func(*args)
  File "F:\Python\PyAnzhuang\lib\urllib\request.py", line 579, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-11 17:58:05 | 显示全部楼层
上代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-12 10:02:03 | 显示全部楼层
import urllib.request
import json
import pickle
import easygui as g
import sys

pickle_file = open('F:\百度云_下载\Python\查询天气\city_date.pkl','rb')
city = pickle.load(pickle_file)

g.msgbox('由于中国天气网接口原因,只能查询到3月4号的天气,请大家谅解~','温馨提示')

while 1:
    password=g.enterbox(msg='请输入城市:', title=' ', default='', strip=True, image=None, root=None)
    name1=city[password]
    File1 =urllib.request.urlopen('http://m.weather.com.cn/data/'+name1+'.html')#打开url
    weatherHTML= File1.read().decode('utf-8')#读入打开的url
    weatherJSON = json.JSONDecoder().decode(weatherHTML)#创建json
    weatherInfo = weatherJSON['weatherinfo']
    #打印信息
    information = ''
    information =  ('城市' + weatherInfo['city'] + '\n' +
                '时间:' + weatherInfo['date_y'] + '\n' +
                '24小时天气:' + '\n' +
                '温度:' + weatherInfo['temp1'] + '\n' +
                '天气:'+weatherInfo['weather1'] + '\n' +
                '风速:'+ weatherInfo['wind1'] + '\n' +
                '紫外线:'+ weatherInfo['index_uv'] + '\n' +
                '穿衣指数:', weatherInfo['index_d'])
    g.msgbox(information)
    i = g.ccbox('还要查询其它城市吗?',choices=('必须的!','不玩了!'))
    if i:
        pass
    else:
        sys.exit(0)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-12 13:24:55 From FishC Mobile | 显示全部楼层
403拒绝访问?加个headers呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-12 13:38:25 | 显示全部楼层
刚看试了一下(http://m.weather.com.cn/data/重庆.html)浏览器也是提示403
发现他们是用城市编码请求数据的,如:http://m.weather.com.cn/mweather/101040100.shtml
你可以试试!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-23 21:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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