Wechat public account: Radish hodgepodge, pay attention to learn more original content.

The Mid-Autumn Festival is approaching. I wonder if you guys have any idea where to go. But to be honest, every holiday, everywhere is a sea of people, that sentence “I can not move”, but also from time to time in my ear.

But come back, holiday travel, in addition to the human factors, weather factors are not to consider it, today, we will take you to see, the Mid-Autumn small holiday, where suitable for travel.

To get the data

To obtain the data, we grab it directly from the Chinese weather website. Some API on the Internet, some information is not very complete, can only obtain the data of the last 3 days, and some need to pay, it is better to grab it by ourselves.

www.weather.com.cn/weather15d/…

The website does not do any restrictions, we capture the data, just need to control the frequency of access, do not affect the normal operation of others can.

You also need to prepare four data files

  • List of provincial capitals, Provincial_Capital
  • National City ID information table, China-city-list.csv
  • A list of famous attractions
  • National scenic spot ID information table, China-scenic -list.txt

The process of grasping is no longer detailed, directly give the complete code

# coding = utf-8
"""
@author: zhou
@time:2019/9/5 14:36
@File: main.py
"""

import requests
from bs4 import BeautifulSoup
import time
import os


def get_data(name, city, code):
    print("Downloading data for city %s" % city)
    url = 'http://www.weather.com.cn/weather15d/%s.shtml' % code[2:]
    res = requests.get(url).content.decode()
    content = BeautifulSoup(res, "html.parser")
    weather_list = content.find('ul', attrs={'class': 't clearfix'}).find_all('li')
    items = map(parse_item, weather_list)
    save_to_csv(name, city, items)
    time.sleep(1)


def parse_item(item):
    time = item.find('span', attrs={'class': 'time'}).text
    wea = item.find('span', attrs={'class': 'wea'}).text
    tem = item.find('span', attrs={'class': 'tem'}).text
    wind = item.find('span', attrs={'class': 'wind'}).text
    wind_level = item.find('span', attrs={'class': 'wind1'}).text
    result = {
        "time": time,
        "wea": wea,
        "tem": tem,
        "wind": wind,
        "wind_level": wind_level
    }
    return result


def save_to_csv(name, city, data):
    if not os.path.exists('%s_data.csv' % name):
        with open('%s_data.csv' % name, 'a+', encoding='utf-8') as f:
            f.write('city,time,wea,tem,wind,wind_level\n')
            for d in data:
                try:
                    row = '{}, {}, {}, {}, {}, {}'.format(city,
                                                     d['time'],
                                                     d['wea'],
                                                     d['tem'],
                                                     d['wind'],
                                                     d['wind_level'])
                    f.write(row)
                    f.write('\n')
                except:
                    continue
    else:
        with open('%s_data.csv' % name, 'a+', encoding='utf-8') as f:
            for d in data:
                try:
                    row = '{}, {}, {}, {}, {}, {}'.format(city,
                                                     d['time'],
                                                     d['wea'],
                                                     d['tem'],
                                                     d['wind'],
                                                     d['wind_level'])
                    f.write(row)
                    f.write('\n')
                except:
                    continue


if __name__ == '__main__':
    import pandas as pd
    provincial = pd.read_csv('provincial_capital')
    china_city_code = pd.read_csv('china-city-list.csv')
    china_scenic_code = pd.read_csv('china-scenic-list.txt', sep='\t')
    china_scenic_code.columns = ['ID'.'name'.'area'.'provincial']
    attraction = pd.read_csv('attractions')
    provincial_data = pd.DataFrame()
    attraction_data = pd.DataFrame()

    # capture
    for i in provincial['city'].values.tolist():
        for j in china_city_code['City_CN'].values.tolist():
            if j == i:
                provincial_data = pd.concat([china_city_code[china_city_code['City_CN'] == j], provincial_data])

    for city in provincial_data['City_CN'].values.tolist():
        city_id = provincial_data[provincial_data['City_CN'] == city]['City_ID'].values.tolist()[0]
        get_data('weather', city, city_id)

    # spot grab
    for a in attraction['attractions'].values.tolist():
        for c in china_scenic_code['name'].values.tolist():
            if c == a:
                attraction_data = pd.concat([china_scenic_code[china_scenic_code['name'] == c], attraction_data])

    for attrac in attraction_data['name'].values.tolist():
        city_id = attraction_data[attraction_data['name'] == attrac]['ID'].values.tolist()[0]
        get_data('attraction', attrac, city_id)

Copy the code

Provincial weather analysis

First, let’s take a look at the weather in provincial capitals. After all, provincial capitals are the centers of each province and the key cities for tourism.

Precipitation and Temperature

As for the probability of precipitation, IF the forecast is rain, the probability of precipitation is set as 80, and if the forecast is sunny, the probability of precipitation is 20.

weather_dict = {
    "snow": 100."rain": 80."cloud": 50."overcast": 60."sun": 20
}
Copy the code

On the day of Mid-Autumn Festival, precipitation and temperature in various provincial capitals

It can be seen that the weather is not beautiful in most cities on this day, and the probability of precipitation is very large. And the temperature, the probability of precipitation in the city, the temperature is not very high, morning and evening travel, may also be very cool oh. The highest temperature should be Nanchang, can reach 30°C, a sunny day, is not to see the revolution of the holy land?

Let’s look at the water drop and temperature more visually through a biaxial diagram

It seems that after entering September, the general temperature of the country is slowly falling, the temperature is suitable for travel, but it will be accompanied by continuous drizzle.

Let’s take a look at the weather in major cities in the week before and after the Mid-Autumn Festival

Beijing

Beijing’s temperature is still relatively stable, without too much fluctuation, may be able to hold a thin coat in the morning and evening, but these days, should be overcast, there will not be too good sunshine.

Shanghai

Shanghai has more precipitation than Beijing, but the temperature is similar.

hangzhou

The average temperature of Hangzhou is still higher, and the probability of precipitation is also higher. After all, it is a typical coastal city in southeast China. Do you look forward to the West Lake on rainy days?

chengdu

It rains almost every day in Chengdu, so why go out to see pandas? This is a problem!

Weather of Famous Scenic Spots

Now let’s take a look at the weather conditions of some famous scenic spots. There are so many scenic spots that we can only simply list some of the most famous places to see.

precipitation

Most of the spots I chose will have rain, but there will also be sunny spots. For example, mount Huangshan and Badaling Great Wall, it is expected to be sunny, to climb the Great Wall and Mount Huangshan, is a good choice. And the beautiful Jiuzhaigou and West Lake, although it may rain, but walking on rainy days, is also a kind of fun.

Precipitation and Temperature

Let’s take a look at the temperature

I don’t know why the temperature in Chengde is so low, I feel it is not appropriate to go to the summer vacation, while changbai Mountain is only 7°C, do you panic?

Precipitation and temperature distribution

Finally, let’s take a look at the distribution of precipitation and temperature on Mid-Autumn Day

precipitation

In September, precipitation in the southeast coastal areas increased significantly, and it was also rainy in Beijing and Tianjin. Is this an autumn rain and a cold rhythm?

The temperature

The southeast half of the wall, the temperature is more suitable, now the weather, neither cold nor hot, it is a good travel temperature.

Well, that’s all for today’s analysis. So, where is your final choice for the Mid-Autumn Festival?

This article has been exclusively authorized to script home (ID: Jb51net) public account for publication