031-永久存储-课件中天气查询
按照小甲鱼的步骤将城市数据保存为pkl文件,之后运行主程序,报错:403应该是访问被拒,是不是因为这个爬虫代码太久之前的,所以无论访问哪一个网站几乎都会被拒绝(除过百度)?
拿代码说话 代码呢? 本帖最后由 亚瑟与精灵 于 2019-12-4 09:50 编辑
import urllib.request
import json
import pickle
pickle_file = open('D:\日志\code\city_data.pkl','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 ('按任意键退出:') 本帖最后由 亚瑟与精灵 于 2019-12-4 09:50 编辑
zltzlt 发表于 2019-12-3 20:16
代码呢?
代码已经贴出来了 网址变了,还加了防爬措施
import urllib.request
import pickle
import time
import json
pickle_file = open('city_data.pkl', 'rb')
city = pickle.load(pickle_file)
password = input('请输入城市:')
cityid = city
url = r'http://d1.weather.com.cn/weather_index/{}.html?_={}'.format(cityid, str(int(time.time() * 1000)))
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36',
'Referer': 'http://www.weather.com.cn/weather1d/{}.shtml'.format(cityid)
}
request = urllib.request.Request(url=url, headers=headers)
res = urllib.request.urlopen(request)
js = res.read().decode('utf-8')
print(js)
#分析数据自己写吧 kaohsing 发表于 2019-12-4 11:50
可以讲解一下您url;headers;request吗{:5_93:} 本帖最后由 kaohsing 于 2019-12-4 16:40 编辑
url是通过抓包获得的。
至于urllib.request.Request,三言两语,讲不清,请百度 kaohsing 发表于 2019-12-4 11:50
谢谢,这是我的分析数据
import urllib.request
import pickle
import time
import json
pickle_file = open('F:\\TestRoad\\2021-04--小甲鱼Python学习\\47天气预报\\city_data.pkl','rb')
city = pickle.load(pickle_file)
password = input('请输入城市:')
cityid = city
url = r'http://d1.weather.com.cn/weather_index/{}.html?_={}'.format(cityid, str(int(time.time() * 1000)))
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36',
'Referer': 'http://www.weather.com.cn/weather1d/{}.shtml'.format(cityid)
}
request = urllib.request.Request(url=url, headers=headers)
res = urllib.request.urlopen(request)
js = res.read().decode('utf-8')
#js = res.read().decode('unicode_escape')
weather_info = js.split(';',1)
weather_info = weather_info.split('=', 1)
weather_dict = json.loads(weather_info)
print('天气:', weather_dict.get('weatherinfo').get('weather'))
#print(weather_dict)
页:
[1]