“最美天气”版本II
抓取实时天气前需要首先获得所有地区的cityCode,因为在最后queryWeather的时候需要传递这个参数。
最美天气获取全部cityCode及cityName的方法:
1 2 3 4 5 6 7 8 | 1. 获取 34 个省及直辖市的cityCode、cityName http: / / www.zuimeitianqi.com / zuimei / myCity form:flg = 0 json:proviceList 2. 根据proviceList里的cityCode和cityName获取各省及直辖市cityList http: / / www.zuimeitianqi.com / zuimei / getCity form:cityCode cityName json:cityList 3. 根据cityList里的cityCode和cityName获取地级市cityList http: / / www.zuimeitianqi.com / zuimei / getCity form:cityCode cityName json:cityList |
程序功能基本上实现了,第一次运行时缺少"zuimei_cities.db"需要抓取,会耗费比较长的时间。
再次运行的时候就非常的快了。博客好像不可以上传附件,就算了。
程序运行截图如下:
最后附上程序代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | import requests import pickle """ 利用“最美天气”抓取即时天气情况 @author likui_911@163.com """ class ZuiMei(): def __init__( self ): # Content-Type在这里非常关键,一定要设定 self .headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36' , 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' , 'Accept' : 'application/json, text/javascript, */*; q=0.01' , 'Accept-Encoding' : 'gzip, deflate' , 'Accept-Language' : 'zh-CN,zh;q=0.8' } self .zuimei_cities = {} try : # 判断zuimei_cities文件是否存在,不存在则初始化生成 with open ( 'zuimei_cities.db' , 'rb' ) as f: self .zuimei_cities = pickle.load(f) except FileNotFoundError: print ( '初始化中,请耐心等待程序运行...' ) with open ( 'zuimei_cities.db' , 'wb' ) as f: self .zuimei_cities = self .initial_cities() pickle.dump( self .zuimei_cities, f) print ( '初始化完成,可以正常使用了' ) def get_city( self , cityCode, cityName): """ 返回省市下级区县城市列表 """ city_url = "http://www.zuimeitianqi.com/zuimei/getCity" req_data = {} req_data[ 'cityCode' ] = cityCode req_data[ 'cityName' ] = cityName r = requests.post(city_url, data = req_data, headers = self .headers) my_cities = r.json() return my_cities[ 'cityList' ] def query_weather( self , cityCode): """ 返回当前区域的天气字典 actual 实时天气 forecast 预报 air 空气情况 """ weather_url = "http://www.zuimeitianqi.com/zuimei/queryWeather" req_data = {} req_data[ 'cityCode' ] = cityCode r = requests.post(weather_url, data = req_data, headers = self .headers) current_weather = r.json() return current_weather[ 'data' ][ 0 ] def my_city( self ): """ 返回省、直辖市列表 """ province_url = "http://www.zuimeitianqi.com/zuimei/myCity" req_data = { "flg" : "0" } r = requests.post(province_url, data = req_data, headers = self .headers) my_cities = r.json() return my_cities[ 'provinceList' ] def initial_cities( self ): """ 初始化生成cityName:cityCode字典 """ zuimei_cities = {} # 获取省市直辖市 provinces = self .my_city() for province in provinces: cityCode = province[ 'cityCode' ] cityName = province[ 'cityName' ] # 获取地级市 cities = self .get_city(cityCode, cityName) for each_city in cities: cityCode = each_city[ 'cityCode' ] cityName = each_city[ 'cityName' ] # 获取县级地区 counties = self .get_city(cityCode, cityName) for each_county in counties: cityCode = each_county[ 'cityCode' ] cityName = each_county[ 'cityName' ] zuimei_cities[cityName] = [cityCode] return zuimei_cities def query( self , cityName): """ 根据用户输入的cityName,获得实时的天气 """ if cityName not in self .zuimei_cities: print ( '地区不存在,请检查输入' ) else : cityCode = self .zuimei_cities[cityName] weather = self .query_weather(cityCode) actual = weather[ 'actual' ] print ( '%s: %s℃~%s℃ %s' % (cityName, actual[ 'high' ], actual[ 'low' ], actual[ 'desc' ])) if __name__ = = '__main__' : zuimei = ZuiMei() zuimei.query( '香港' ) |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· Java轻量级代码工程
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战