这里使用requests模块进行高德地图城市天气信息的爬取
1、首先我们需要找到高德地图的城市API接口
打开高德地图,按F12开发者工具下找到cityList?version=201981715这个地址,就是城市API接口地址,如果城市信息无法显示,将后面的version参数删除或者改一下日期
2、再次找到高德地图的城市天气API接口
3、根据城市API接口和城市天气API接口进行分析
查看天气接口可以发现它的接口规律,后面是跟着城市的编码
所以我们必须先抓取出城市的编码信息,也就是adcode对应的值
import requests
import json
# 获取城市数据信息
def get_request_info(url,headers):
response = requests.get(url,headers=headers)
response_json = response.json()
return response_json
# 获取全国所有城市的编码及名称信息
def get_city_info(response):
# 存储城市信息
city_list = []
# 循环出城市信息 # {'adcode': '152900', 'name': '阿拉善盟'}
for value in response['data']['cityByLetter'].values():
for city in value:
print(city)
# city_name = city.get('name')
# adcode = city.get('adcode')
city_list.append(city)
return city_list
4、根据抓取的城市编码,爬取天气
# 爬取天气
def get_weather_info(city_list,headers):
# 取出城市名称和编码
for city in city_list:
city_name = city.get('name')
adcode = city.get('adcode')
# 请求天气接口,并将城市编码传入url,获取指定城市的天气信息
weather_url = 'https://www.amap.com/service/weather/?adcode=%s'%adcode
# 进行get请求,返回响应
weather_response = requests.get(weather_url,headers=headers).text
# 将返回的json字符串转为python字典类型
weather_json = json.loads(weather_response)
# weather = weather_json['data']['data'][0]['live']['weather_name'] # keyerror
# weather_data = weather_json.get('data').get('data')[0].get('live').get('weather_name')
try:
# 当前天气情况
# 当前天气名称
weather_name_live = weather_json.get("data").get('data')[0].get("live").get('weather_name')
# 当前天气气温
weather_temperature_live = weather_json.get("data").get('data')[0].get("live").get('temperature')
# 预测明日天气情况
# 明日天气数据
tomorrow_forecast_data = weather_json.get("data").get('data')[1].get("forecast_data")[0]
# 明日天气名称
tomorrow_weather_name = tomorrow_forecast_data["weather_name"]
# 明日最高最低气温
tomorrow_max_temp = tomorrow_forecast_data["max_temp"]
tomorrow_min_temp = tomorrow_forecast_data["min_temp"]
# 明日风向及风力
wind_direction_desc = tomorrow_forecast_data["wind_direction_desc"]
wind_power_desc = tomorrow_forecast_data["wind_power_desc"]
except TypeError as e:
print(e)
finally:
info = f'{city_name} 今天的天气:{weather_name_live},当前气温:{weather_temperature_live};明天的天气为:{tomorrow_weather_name},' \
f'最高气温:{tomorrow_max_temp},最低气温:{tomorrow_min_temp},风向:{wind_direction_desc},风力:{wind_power_desc}'
print(info+'\n')
with open('天气预报.txt','a',encoding='utf-8') as f:
f.write(info+'\n')
完整代码:
import requests
import json
# 获取城市数据信息
def get_request_info(url,headers):
response = requests.get(url,headers=headers)
response_json = response.json()
return response_json
# 获取全国所有城市的编码及名称信息
def get_city_info(response):
# 存储城市信息
city_list = []
# 循环出城市信息 # {'adcode': '152900', 'name': '阿拉善盟'}
for value in response['data']['cityByLetter'].values():
for city in value:
print(city)
# city_name = city.get('name')
# adcode = city.get('adcode')
city_list.append(city)
return city_list
# 爬取天气
def get_weather_info(city_list,headers):
# 取出城市名称和编码
for city in city_list:
city_name = city.get('name')
adcode = city.get('adcode')
# 请求天气接口,并将城市编码传入url,获取指定城市的天气信息
weather_url = 'https://www.amap.com/service/weather/?adcode=%s'%adcode
# 进行get请求,返回响应
weather_response = requests.get(weather_url,headers=headers).text
# 将返回的json字符串转为python字典类型
weather_json = json.loads(weather_response)
# weather = weather_json['data']['data'][0]['live']['weather_name'] # keyerror
# weather_data = weather_json.get('data').get('data')[0].get('live').get('weather_name')
try:
# 当前天气情况
# 当前天气名称
weather_name_live = weather_json.get("data").get('data')[0].get("live").get('weather_name')
# 当前天气气温
weather_temperature_live = weather_json.get("data").get('data')[0].get("live").get('temperature')
# 预测明日天气情况
# 明日天气数据
tomorrow_forecast_data = weather_json.get("data").get('data')[1].get("forecast_data")[0]
# 明日天气名称
tomorrow_weather_name = tomorrow_forecast_data["weather_name"]
# 明日最高最低气温
tomorrow_max_temp = tomorrow_forecast_data["max_temp"]
tomorrow_min_temp = tomorrow_forecast_data["min_temp"]
# 明日风向及风力
wind_direction_desc = tomorrow_forecast_data["wind_direction_desc"]
wind_power_desc = tomorrow_forecast_data["wind_power_desc"]
except TypeError as e:
print(e)
finally:
info = f'{city_name} 今天的天气:{weather_name_live},当前气温:{weather_temperature_live};明天的天气为:{tomorrow_weather_name},' \
f'最高气温:{tomorrow_max_temp},最低气温:{tomorrow_min_temp},风向:{wind_direction_desc},风力:{wind_power_desc}'
print(info+'\n')
with open('天气预报.txt','a',encoding='utf-8') as f:
f.write(info+'\n')
if __name__ == "__main__":
# 城市接口url
url = 'https://www.amap.com/service/cityList'
# 请求头
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'}
# 获取城市信息
response = get_request_info(url,headers)
# 获取全国城市列表
city_list = get_city_info(response)
# 获取城市天气
get_weather_info(city_list,headers)
效果: