import time
import requests
from prettytable import PrettyTable
'''
查询当前地点天气的url:https://www.amap.com/service/weather?adcode=110000
各城市对应code的url:https://www.amap.com/service/cityList?version=20207
备注:这两个url可以从Network中查看到
'''
def get_location_dic(location_dic_url,province):
list = ['北京市', '天津市', '河北省', '山西省', '内蒙古自治区', '辽宁省', '吉林省', '黑龙江省', '上海市', '江苏省', '浙江省', '安徽省', '福建省', '江西省', '山东省', '河南省',
'湖北省', '湖南省', '广东省', '广西壮族自治区', '海南省', '重庆市', '四川省', '贵州省', '云南省', '西藏自治区', '陕西省', '甘肃省', '青海省', '宁夏回族自治区',
'新疆维吾尔自治区', '台湾省', '香港特別行政區', '澳門特別行政區']
if province not in list:
print('_'*100)
print(':( 输入错误!请输入正确的省份名称')
print('提示:可输入的省份名称为:')
print(list)
else:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36'}
response = requests.get(location_dic_url,headers=headers)
response.encoding = response.apparent_encoding
data = response.json()['data']['cityData']['provinces']
provinces_code_dic = {}
for d in data:
provinces_code_dic[data[d]['label']] = data[d]['adcode']
cities_data = data[provinces_code_dic[province]]['cities']
if not cities_data:
cities_data = [data[provinces_code_dic[province]]]
return cities_data
return ''
def get_today_data(url,location_code):
weather_url = url + location_code
response = requests.get(weather_url)
response.encoding = response.apparent_encoding
today_data = response.json()['data']['data'][0]
return today_data
def get_wheather(today_data):
wheather = {
'day_wheather': {
'max_temp': today_data['forecast_data'][0]['max_temp'],
'min_temp': today_data['forecast_data'][0]['min_temp'],
'weather_name': today_data['forecast_data'][0]['weather_name'],
'wind_power_desc': today_data['forecast_data'][0]['wind_power_desc'],
'wind_direction_desc': today_data['forecast_data'][0]['wind_direction_desc']
},
'night_wheather': {
'max_temp': today_data['forecast_data'][1]['max_temp'],
'min_temp': today_data['forecast_data'][1]['min_temp'],
'weather_name': today_data['forecast_data'][1]['weather_name'],
'wind_power_desc': today_data['forecast_data'][1]['wind_power_desc'],
'wind_direction_desc': today_data['forecast_data'][1]['wind_direction_desc']
}
}
return wheather
if __name__ == '__main__':
while True:
province = input('请输入省份名称:')
print('正在爬取,请稍后...')
print('')
url = 'https://www.amap.com/service/weather?adcode='
location_dic_url = 'https://www.amap.com/service/cityList?version=20207'
all_info = []
location_dic_all = get_location_dic(location_dic_url,province)
if location_dic_all:
location_dic = [
{
base['name']:base['adcode'] for base in location_dic_all
}
]
locations = location_dic[0].keys()
for location in locations:
today_data = get_today_data(url,location_dic[0][location])
wheather = get_wheather(today_data)
all_info.append(
{
'location':location,
'day_wheather':wheather['day_wheather'],
'night_wheather':wheather['night_wheather']
}
)
today = today_data['forecast_date']
weekday = str(today_data['weekday'])
weekday_dic = {
'1':'星期二',
'2':'星期三',
'3':'星期四',
'4':'星期五',
'5':'星期六',
'6':'星期日',
'7':'星期一',
}
tb = PrettyTable()
tb.field_names = ['城市','早晚','天气','最高温度','最低温度','风力']
for x in all_info:
tb.add_row([x['location'],'白天',x['day_wheather']['weather_name'],x['day_wheather']['max_temp'],x['day_wheather']['min_temp'],x['day_wheather']['wind_direction_desc'] + ' ' + x['day_wheather']['wind_power_desc']])
tb.add_row(['','夜晚',x['night_wheather']['weather_name'],x['night_wheather']['max_temp'],x['night_wheather']['min_temp'],x['night_wheather']['wind_direction_desc'] + ' ' + x['night_wheather']['wind_power_desc']])
print('今天是%s %s。%s天气如下:'%(today,weekday_dic[weekday],province))
print(tb)
print('*'*100)
复制代码
近期评论