Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Weather Voice broadcast Assistant by Python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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. "

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report