In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use Python to analyze the weather of the Mid-Autumn Festival. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
The Mid-Autumn Festival is approaching. I wonder if all my friends have decided where to go to play. But to tell you the truth, every holiday, there is a sea of people everywhere, the sentence "I can't move", I can still sing it!
But then again, apart from human factors, weather factors should also be considered for holiday outings. Today, we will use Python reptiles to show you which places are suitable for travel during Mid-Autumn Festival short holiday.
Get data
Data acquisition, directly from the Chinese weather website crawled, some API on the network, some information is not very complete, only access to the last 3 days of data, and some need to pay, not as happy as their own capture.
The website does not have any restrictions, when we grasp the data, we just need to control the access frequency, do not affect the normal operation of others.
At the same time, four data files need to be prepared.
List of provincial capitals, provincial_capital
National id Information Table for cities, china-city-list.csv
Name list of famous scenic spots, attractions
National Scenic spot id Information Table, china-scenic-list.txt
The process of crawling is no longer explained in detail, but the complete code is given directly.
# coding = utf-8 "" @ author: zhou@time:2019/9/5 14:36@File: main.py "" import requestsfrom bs4 import BeautifulSoupimport timeimport osdef 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 resultdef save_to_csv (name, city, data): if not os.path.exists ('% sroomdata.csv'% name): with open ('% sroomdata.csv'% name, 'axiomatic, encoding='utf-8') as f: f.write (' city,time,wea,tem,wind,wind_level\ n') for din 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 ('% sroomdata.csv'% name, 'ajar, encoding='utf-8') as f: for din data: try: row =' {}, {} '.format (city, d [' time']) D ['wea'], d [' tem'], d ['wind'], d [' wind_level']) f.write (row) f.write ('\ n') except: continueif _ _ 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 () # the provincial capital grabs 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) # Scenic spot capture for an 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) weather analysis of provincial capital
First of all, let's take a look at the weather in the provincial capital. after all, the provincial capital is the center of each province and a key city for tourism.
Precipitation and temperature
For the probability of precipitation, I take that if the forecast is rainy, then set the precipitation probability to 80, if the forecast is sunny, the precipitation probability is 20. 5%.
Weather_dict = {"snow": 100, "rain": 80, "cloud": 50, "overcast": 60, "sun": 20}
On the 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 high. As for the temperature, the city with high probability of precipitation, the temperature is not very high, sooner or later, it may be very cold. The highest temperature should be in Nanchang, which can reach 30 °C. on a sunny day, would you like to visit the Holy Land of the Revolution?
Next, we will use a biaxial diagram to see the falling water and temperature more intuitively.
It seems that after entering September, the general temperature across the country is slowly falling, and the temperature is suitable for travel, but it will be accompanied by continuous drizzle.
Let's take a look at the weather in several major cities in the week before and after the Mid-Autumn Festival.
Beijing
The temperature in Beijing is still relatively stable, without much fluctuation, it may be possible to live in hold with a thin coat sooner or later, but these days, it should be cloudy and there will not be too good sunshine.
Shanghai
The probability of precipitation in Shanghai is higher than that in Beijing, but the temperature is not much different.
Hangzhou
The average temperature in Hangzhou is still higher, and the probability of precipitation is also higher. after all, a typical southeast coastal city, the West Lake on rainy days, do you look forward to it?
Chengdu
Chengdu basically rains every day, so go out to see giant pandas, this is a problem!
Weather of famous scenic spots
Next, let's take a look at the weather conditions of some famous scenic spots. I have so many scenic spots that I can only briefly list some of the most famous places to see.
Precipitation situation
Among the scenic spots I have selected, most of them will have precipitation, but there will also be sunny places.
For example, Huangshan and Badaling Great Wall are expected to be sunny, so it is a good choice to climb the Great Wall and Huangshan.
And the beautiful Jiuzhaigou and West Lake, although it will rain, but walking on a rainy day can be regarded as a kind of interest.
Precipitation and temperature
Let's take a look at the temperature in various places.
I do not know why the temperature in Chengde is so low, it feels that it is not appropriate to go to summer, while Changbai Mountain is only 7 °C, is it panicked?
Precipitation and temperature distribution
Finally, let's take a look at the distribution of precipitation and temperature on the Mid-Autumn Festival.
Precipitation
In September, there has been a marked increase in precipitation along the southeast coast, and the Beijing-Tianjin area is also overcast and rainy. Is this a cold rhythm of autumn rain?
Temperature
Southeast half of the wall, the temperature is still relatively suitable, in the current weather, neither hot nor cold, it is a good temperature for travel.
Dear partner, where are you in the Mid-Autumn Festival? How many people lie dead at home like me!
The above is how to use Python to analyze the weather of the Mid-Autumn Festival. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.