031泡菜求助
为什么课上下载的源代码在pycharm上面运行就不行了呢?python小白找不出错误!
直接下载源代码运行后的结果如下:
请输入城市:汕头
Traceback (most recent call last):
File "D:/Desktop/小甲鱼/31-40源代码/天气查询课件源代码.py", line 2452, in <module>
File1 =urllib.request.urlopen('http://m.weather.com.cn/data/'+name1+'.html')#打开url
File "D:\python\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "D:\python\lib\urllib\request.py", line 532, in open
response = meth(req, response)
File "D:\python\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "D:\python\lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
File "D:\python\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "D:\python\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 爬虫被网站反爬了,需要加上 headers 。(这个知识点等你学到爬虫那块就知道,现在先不用管)
代码发上来,帮你改改。(可能会太长) 被反爬了? import urllib.request
import json
#建立城市字典
#中间城市信息被我删掉了
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 ('按任意键退出:') zltzlt 发表于 2020-8-11 18:46
爬虫被网站反爬了,需要加上 headers 。(这个知识点等你学到爬虫那块就知道,现在先不用管)
代码发上 ...
感谢 zltzlt 发表于 2020-8-11 18:46
爬虫被网站反爬了,需要加上 headers 。(这个知识点等你学到爬虫那块就知道,现在先不用管)
代码发上 ...
import urllib.request
import json
#建立城市字典
#中间城市信息被我删掉了
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 ('按任意键退出:') import urllib.request
import json
#建立城市字典
#中间城市信息被我删掉了
password=input('请输入城市:')
name1=city
req = urllib.request.Request('http://m.weather.com.cn/data/'+name1+'.html', headers={'User-Agent': 'Mozilla/5.0'})
File1 =urllib.request.urlopen(req)#打开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 ('按任意键退出:')
页:
[1]