tao20102711 发表于 2018-5-31 21:24:40

零基础入门学习Python——第31讲问题

【视频教程】零基础入门学习Python
第31讲,我输入城市报错:
Traceback (most recent call last):
File "D:\Program Files\Python365\mine\pickle_city\城市.py", line 11, in <module>
    File1 =urllib.request.urlopen('http://m.weather.com.cn/data/'+name1+'.html')#打开url
File "D:\Program Files\Python365\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
File "D:\Program Files\Python365\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
File "D:\Program Files\Python365\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
File "D:\Program Files\Python365\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
File "D:\Program Files\Python365\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
File "D:\Program Files\Python365\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


下面是源程序
import urllib.request
import json
import pickle
pickle_file=open('D:\\Program Files\\Python365\\mine\\pickle_city\\city_data_2.txt','rb')

city=pickle.load(pickle_file)

password=input('请输入城市:')
name1=city
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']
#打印信息
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 ('按任意键退出:')

BngThea 发表于 2018-6-1 08:58:27

目测是被墙了{:10_254:}

tao20102711 发表于 2018-6-1 20:21:26

啥意思?

凌九霄 发表于 2018-6-12 11:48:44

提示很明白:urllib.error.HTTPError: HTTP Error 403: Forbidden
造成这种错误的原因,可能网站有反爬虫机制,你得模拟从浏览器提交数据去取数,也就是说,你要在请求上加头信息

tao20102711 发表于 2018-6-19 09:00:29

凌九霄 发表于 2018-6-12 11:48
提示很明白:urllib.error.HTTPError: HTTP Error 403: Forbidden
造成这种错误的原因,可能网站有反爬虫 ...

表示现在还不会啊

凌九霄 发表于 2018-6-19 09:58:41

本帖最后由 凌九霄 于 2018-6-19 11:00 编辑

tao20102711 发表于 2018-6-19 09:00
表示现在还不会啊

看了下,中国天气查询页面的接口有问题,原接口是返回json数据的,可能要另找一个免费的有效接口

shiyi10871993 发表于 2018-11-2 14:41:10

我也是这样
页: [1]
查看完整版本: 零基础入门学习Python——第31讲问题