In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to use Python to make a simple weather broadcasting system". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Tools
Python3.7
Pycharm
Pyttsx3: voice broadcast Library
Weather data source
Looking for a weather website
For example, if we want to inquire about the weather of a place, we can see the results after entering the place name.
We can see that the url of the website will change:
The weather information of each city url is to add the city pinyin after the website. In this case, you can use function parameters to solve the city input problem.
Code implementation # def get_weather (city): # Weather website URL string url = 'https://www.tianqi.com/' + city +' /'# camouflage browser vest headers = {'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64) X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.58'} # initiate a network request and get the web page code website = requests.get (url=url Headers=headers) data = etree.HTML (website.text) # data preprocessing # xpath parsing page Weather data weather_list = data.xpath ('/ / dl [@ class= "weather_info"] / / text ()') weather_text = 'Welcome to Weather broadcast Assistant Here's the weather you want to know about the city:\ n` for text in weather_list: weather_text + = text if weather_text.find ('[switch cities]'): weather_text = weather_text.strip ('[switch cities]') print (weather_text) return weather_text
In this way, the weather data of each city can be obtained through the crawler, and then the voice object instance can be built through pyttsx3, and the weather text can be put in and run.
If _ _ name__ = ='_ _ main__': city = input ('Please enter your city For example, Changsha: changsha-- >'). Strip () weather_info = get_weather (city) # weather_info to get the captured weather text weather = pyttsx3.init () # initialize the speaking object weather.say (weather_info) # set the speaking content weather.runAndWait () # start the speaking fuck That's all for "how to use Python to make a simple Weather broadcast system". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.