How to realize the Weather Voice broadcast Assistant by Python
This article mainly introduces Python how to achieve weather voice broadcast assistant, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.
Environment installation: Python3.6, pycharm2021, and self-contained modules, etc.
Pip install-I https://pypi.douban.com/simple/ requests pip install-I https://pypi.douban.com/simple/ opencv-python
It is mainly divided into three parts:
(1) obtain daily weather conditions:
Def get_weather (): url = 'http://www.weather.com.cn/weather/101290101.shtml' response = requests.get (url) response.encoding =' utf-8' response = response.text # get page html = etree.HTML (response) day_weather = 'Weather conditions:' + html.xpath ('/ * [@ id= "7d"] / ul/li [1] / p [1] / text () ') [0] +'\ n' # get the weather Daytime weather high = html.xpath ('/ * [@ id= "7d"] / ul/li [1] / p [2] / span/text ()') low = html.xpath ('/ / * [@ id= "7d"] / ul/li [1] / p [2] / i/text ()') # get the corresponding two temperatures # because the page changes slightly at night, so use conditional statements To exclude the change caused by bug if high = = []: day_temperature = 'outdoor temperature:' + low [0] +'\ n' else: day_temperature = 'outdoor temperature:' + low [0] .replace ('℃' '') +'~'+ high [0] + '℃\ n' # get temperature # get two wind directions wind_1 = html.xpath ('/ * [@ id= "7d"] / ul/li [1] / p [3] / em/span [1] / @ title') wind_2 = html.xpath ('/ / * [@ id= "7d"] / ul/li [1] / p [3] / em/span [2) ] / @ title') # because sometimes There will be two winds in the same direction. So use conditional statements to exclude if wind_2 = = []: wind = wind_1 [0] +'\ n' elif wind_1 [0] = = wind_2 [0]: wind = wind_1 [0] +'\ n' else: wind = wind_1 [0] + 'turn' + wind_2 [0] +'\ n' # because wind levels sometimes appear. "