高德地图城市天气信息爬取

这里使用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)

效果:
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孜孜孜孜不倦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值