Use Python to get the weather forecast information of the city, well done (30)

Hello children, hello big friends!

I'm Cat Girl, a primary school student who fell in love with Python programming.

Welcome to learn Python with cat girl.

today's topic

How to use Python to obtain future weather information according to the weather API?

The wxpy library was easy to use before, and it could be used to push the weather forecast information to concerned WeChat friends.

When using this library, I encountered an error report. Netizens mentioned that this library has not been updated for a long time, so it is not recommended to use it.

Therefore, today we omit the step of pushing the obtained weather forecast information to friends, and just manually copy, paste, and send it.

Weather API interface

How do we know the future weather in our local cities?

This information is obtained by requesting the third-party platform.

for example

You ask it what the weather is like in a certain city on a certain day?

The website will return the weather of a certain day in the queried city.

For example, enter the following information in the browser:

http://t.weather.sojson.com/api/weather/city/101020100

you will see:

What is returned here is the weather conditions in Shanghai for the next five days. The results after parsing with Python are as follows:

Many friends are not in Shanghai, how do you check the weather in your city?

Or a friend is in Shanghai, but wants to check the weather in a certain district in Shanghai, how can I check it?

You only need to modify the city_code, that is, change 101020100 (Shanghai) to the city_code of the city you want to query.

How to get the city_code of the local city or region?

You can search for it.

If you don't find it, or you are too lazy to check, you can also read this article of the official account with the same name (Learning Python with Cat Girl).

Cat sister put more than 400 city_codes in this article.

There are 447 city_codes for the public.public.account (Learning Python with Cat Girl).

Python parsing data

Python is good at data processing, requesting data from the server, getting the data, and parsing the data.

These are a piece of cake for Python!

Parse the returned Json format data into a format that is easy for humans to read.

That's all. In the end, Maomei shared the debugged code:

import requestsfrom wxpy import *import json
def get_weather(url):    r=requests.get(url)    data=json.loads(r.text)    city = data['cityInfo']['city']    weather=data['data']['forecast']    return city,weather
def get_content_send(city,weather):    all_day=[]    content_send=""    for i in range(0,6,1):        content=weather[i]        every_day=[]        every_day.append(city+"天气情况:")        every_day.append(content['ymd']+' '+content ['week'])        every_day.append(content['high']+' '+content['low'])        every_day.append(content['fx']+':'+content['fl'])        every_day.append(content['type']+'AQI:'+str(content['aqi']))        every_day.append(content['notice'])        all_day.append(every_day)    for i in range(0,6,1):        for data in all_day[i]:            content_send = content_send+data+'\n'        content_send=content_send+'\n'    return content_send
def send_content(content_send):    bot =Bot(cache_path=True)    my_friend =bot.friends().search('和猫妹学Python')[0]    my_friend.send(content_send)
if __name__ == '__main__':    url ='http://t.weather.sojson.com/api/weather/city/101020100'    city,weather=get_weather(url)    content_send=get_content_send(city,weather)    #send_content(content_send)    print('********************')    print(content_send)

How about it?

Have you tuned in?

Well, let's learn this today!

If you encounter any problems, let's communicate and solve them together.

I'm Cat Girl, see you next time!

 

Guess you like

Origin blog.csdn.net/parasoft/article/details/130163239