|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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/'+ name1+'.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('按任意键退出:')
这个代码为什么会执行错误?
错误代码如下:
请输入城市:北京
Traceback (most recent call last):
File "C:/Users/dell/AppData/Local/Programs/Python/Python36-32/5555555.py", line 26, in <module>
File1 = urllib.request.urlopen('http://m.weather.com.cn/data/'+ name1+'.html')
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 532, in open
response = meth(req, response)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\urllib\request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
|
|