import time

import requests
from prettytable import PrettyTable     A library for printing tables
"' the weather query the current site url:https://www.amap.com/service/weather?adcode=110000 cities corresponding code url: https://www.amap.com/service/cityList?version=20207 note: both the url can see from the Network to the ' ' '

Get the code corresponding to the place name
def get_location_dic(location_dic_url,province) :

    # check whether the input province name is correct
    list = ['Beijing'.Tianjin City.Hebei Province.'Shanxi Province'.Inner Mongolia Autonomous Region.'Liaoning'.'Jilin Province'.'Heilongjiang province'.'Shanghai'.Jiangsu Province.'Zhejiang'.Anhui Province.'Fujian'.'Jiangxi'.'Shandong Province'.'Henan Province'.'Hubei Province'.'Hunan'.'Guangdong'.Guangxi Zhuang Autonomous Region.'Hainan'.Chongqing Municipality.Sichuan Province.'Guizhou'.'Yunnan Province'.'Tibet Autonomous Region'.'Shaanxi'.'Gansu'.Qinghai Province.Ningxia Hui Autonomous Region.'Xinjiang Uygur Autonomous Region'.'Taiwan'.'Hong Kong Special Administrative Region'.'Macao Special Administrative Region']

    if province not in list:
        print('_'*100)
        print(':(typing error! Please enter the correct province name ')
        print('Tip: The name of the province you can enter is:')
        print(list)
    else:
        headers = {
            'User-Agent': 'the 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

        Get json data
        data = response.json()['data'] ['cityData'] ['provinces']

        Create a dictionary to store codes for each province
        provinces_code_dic = {}
        for d in data:
            provinces_code_dic[data[d]['label']] = data[d]['adcode']

        Get basic information about all cities in the specified province
        cities_data = data[provinces_code_dic[province]]['cities']

        # if the municipality does not have a city, return the content containing the code information of the municipality directly above.
        if not cities_data:
            cities_data = [data[provinces_code_dic[province]]]
        return cities_data
    return ' '


# Retrieve key parts from the JSON data returned by Amap
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

Extract the weather information of the current location from the above data and assign the value to the object Wheather
It can be seen from the JSON data that amap's weather data is divided into day and night
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('Please enter province name:')
        print('Climbing, please hold on... ')
        print(' ')
        url = 'https://www.amap.com/service/weather?adcode='
        location_dic_url = 'https://www.amap.com/service/cityList?version=20207'

        # Define an empty list to store weather information for all cities
        all_info = []

        # Obtain the corresponding code and other information of each city
        location_dic_all = get_location_dic(location_dic_url,province)

        if location_dic_all:
            # Remove useless information, keep only city: code
            location_dic = [
                {
                    base['name']:base['adcode'] for base in location_dic_all
                }
            ]

            Select * from city list
            locations = location_dic[0].keys()

            Iterate over the city name, extract the required information and assign it all to 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 = 1-7 = 1-7 = 1-7
            weekday_dic = {
                '1':'Tuesday'.'2':'Wednesday'.'3':'Thursday'.'4':'Friday'.'5':'Saturday'.'6':'Sunday'.'7':'Monday',}Call this module to print the table
            tb = PrettyTable()
            tb.field_names = ['city'.'and'.'the weather'.'Maximum temperature'.'Minimum temperature'.'wind']
            for x in all_info:
                tb.add_row([x['location'].'the day',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([' '.'the night',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('Today is %s %s. %s The weather is as follows:%(today,weekday_dic[weekday],province))
            print(tb)
        print(The '*'*100)
Copy the code