鱼C论坛

 找回密码
 立即注册
查看: 2406|回复: 7

[已解决]新手提问

[复制链接]
发表于 2016-11-27 10:11:29 | 显示全部楼层 |阅读模式
1鱼币
第31课视频 天气查询程序 跟着老师写代码 运行后出现以下提示:
请输入城市:汕头
Traceback (most recent call last):
  File "D:\Program Files (x86)\python\天气查询.py", line 11, in <module>
    File1 =urllib.request.urlopen('http://m.weather.com.cn/data/'+name1+'.html')#打开url
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 471, in open
    response = meth(req, response)
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 581, in http_response
    'http', request, response, code, msg, hdrs)
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 509, in error
    return self._call_chain(*args)
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 443, in _call_chain
    result = func(*args)
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 589, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
>>>

帮忙解答原因,谢谢!
最佳答案
2016-11-27 10:11:30
本帖最后由 ppp111 于 2016-11-27 20:34 编辑

你好  httperror: 403的意思是     Forbidden 服务器已经理解请求,但是拒绝执行它
这次因为网站的反爬虫系统    网站检测认为你是爬虫 这是恶意的   所以它禁止你   
解决办法2个  1.添加请求头headers   一般添加一个User-Agent信息就好了  还是不行就全添加了     把自己伪装成牛栏器
2. 设置代理  if 没出现这个情况即httperror403:
爬取
else:切换一个新的代理   这是伪代码  这2个你可以看看小甲鱼老师的爬虫系列   大概在53课开始讲爬虫了

给你一个参考
在你的代码    name1=city[password] 后加上
  1. header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'}
  2. req = urllib.request.Request(url='http://m.weather.com.cn/data/'+name1+'.html',data=None,headers=headers)
  3. # 接着把File那句改成
  4. File1 = urllib.request.urlopen(req)
复制代码
这样你试试看  不行@我

最佳答案

查看完整内容

你好 httperror: 403的意思是 Forbidden 服务器已经理解请求,但是拒绝执行它。 这次因为网站的反爬虫系统 网站检测认为你是爬虫 这是恶意的 所以它禁止你 解决办法2个 1.添加请求头headers 一般添加一个User-Agent信息就好了 还是不行就全添加了 把自己伪装成牛栏器 2. 设置代理 if 没出现这个情况即httperror403: 爬取 else:切换一个新的代理 这是伪代码 这2个 ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-11-27 10:11:30 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ppp111 于 2016-11-27 20:34 编辑

你好  httperror: 403的意思是     Forbidden 服务器已经理解请求,但是拒绝执行它
这次因为网站的反爬虫系统    网站检测认为你是爬虫 这是恶意的   所以它禁止你   
解决办法2个  1.添加请求头headers   一般添加一个User-Agent信息就好了  还是不行就全添加了     把自己伪装成牛栏器
2. 设置代理  if 没出现这个情况即httperror403:
爬取
else:切换一个新的代理   这是伪代码  这2个你可以看看小甲鱼老师的爬虫系列   大概在53课开始讲爬虫了

给你一个参考
在你的代码    name1=city[password] 后加上
  1. header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'}
  2. req = urllib.request.Request(url='http://m.weather.com.cn/data/'+name1+'.html',data=None,headers=headers)
  3. # 接着把File那句改成
  4. File1 = urllib.request.urlopen(req)
复制代码
这样你试试看  不行@我
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-11-27 13:07:36 | 显示全部楼层
qq213 发表于 2016-11-27 12:41
你把你的源码放上来看一下

好的,谢谢!
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://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 ('按任意键退出:')

我对照着视频里的文本信息,没有什么不同。  
我用Python编译器逐行编译的时候发现下面这行出错,应该是跟系统有关系,我的电脑是win8系统,不知道需要怎么弄才不出错?
File1 =urllib.request.urlopen('http://m.weather.com.cn/data/'+name1+'.html')#
#
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    File1 =urllib.request.urlopen('http://m.weather.com.cn/data/'+name1+'.html')#
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 471, in open
    response = meth(req, response)
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 581, in http_response
    'http', request, response, code, msg, hdrs)
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 509, in error
    return self._call_chain(*args)
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 443, in _call_chain
    result = func(*args)
  File "D:\Program Files (x86)\python\lib\urllib\request.py", line 589, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-11-27 13:50:45 | 显示全部楼层
我尝试了一下,也出现了这个报错,具体的原因可以是这个网站的问题,你以下的图片:
gh.jpg


但我把那字典的数据给写出来了
o.jpg

综上所述,如果网站没有问题,那么理论上应该可以看到,但现在网站有问题,所以.....
我没有办法才把两张图片合在一起,fishc有限制-->qq213是我
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-11-28 07:23:40 | 显示全部楼层
ppp111 发表于 2016-11-27 20:19
你好  httperror: 403的意思是     Forbidden 服务器已经理解请求,但是拒绝执行它。
这次因 ...

好的,非常感谢。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-11-29 20:59:08 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-11-30 01:01:15 | 显示全部楼层
5楼解答很详细
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-2-14 20:09:30 | 显示全部楼层
@ppp111 我的Python是3.5.2的版本,用您提供的那个代码改了之后还是有错误

这个是改过后的程序

import urllib.request
import json
import pickle

pickle_file = open('h:\\module\\天气查询\\city.pkl','rb')
city = pickle.load(pickle_file)

password = input('请输入城市:')
name1 = city[password]

header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'}
req = urllib.request.Request(url='http://m.weather.com.cn/data/'+name1+'.html',data=None,headers=headers)
# 接着把File那句改成
File1 = urllib.request.urlopen(req)


#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 ('按任意键退出:')

运行之后是
请输入城市:上海
Traceback (most recent call last):
  File "H:\module\天气查询\天气查询.py", line 12, in <module>
    req = urllib.request.Request(url='http://m.weather.com.cn/data/'+name1+'.html',data=None,headers=headers)
NameError: name 'headers' is not defined
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 18:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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