爬虫练习项目之一。从高德地图json数据接口获取天气。可以获取某省的所有城市天气。高德地图的这个接口还能获取县城的天气,我只写到了市。有兴趣的朋友可以尝试一下。
完整代码:
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中查看到
'''
# 获取地名对应的code
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
# 获取json数据
data = response.json()['data']['cityData']['provinces']
# 创建字典用于保存各省份的code
provinces_code_dic = {}
for d in data:
provinces_code_dic[data[d]['label']] = data[d]['adcode']
# 获取指定省份的所有城市的code等基础信息
cities_data = data[provinces_code_dic[province]]['cities']
# 直辖市没有城市,此时直接返回上一级包含直辖市code信息的内容。
if not cities_data:
cities_data = [data[provinces_code_dic[province]]]
return cities_data
return ''
# 从高德地图返回的json数据中获取关键部分
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
# 从以上获取的数据中抽取出当前地点的天气信息并赋值给对象wheather
# 从json数据中可以看出,高德地图的天气数据分为白天和夜晚
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 = []
# 获取各城市对应code及其他信息
location_dic_all = get_location_dic(location_dic_url,province)
if location_dic_all:
# 去除没用信息,只保留 城市:code
location_dic = [
{
base['name']:base['adcode'] for base in location_dic_all
}
]
# 提取城市名列表
locations = location_dic[0].keys()
# 遍历城市名称,将所需信息提取出来全部赋给all_info
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'])
# 数据中含有1-7代表星期,通过字典将其和星期一一对应
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)
源代码及打包exe文件下载