“最美天气”版本II
抓取实时天气前需要首先获得所有地区的cityCode,因为在最后queryWeather的时候需要传递这个参数。
最美天气获取全部cityCode及cityName的方法:
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"需要抓取,会耗费比较长的时间。
再次运行的时候就非常的快了。博客好像不可以上传附件,就算了。
程序运行截图如下:
最后附上程序代码:
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('香港')