莫愁前路无知己,天下谁人不识君。。。。
python----天气小编程1
上一篇 /
下一篇 2017-09-08 08:45:42
/ 个人分类:python 小编程
# coding:utf-8
fromseleniumimportwebdriver
importunittest
fromtimeimportsleep
classWeatherReport(unittest.TestCase):
defsetUp(self):
self.dr = webdriver.Chrome()
self.weather,self.the_highest_temperature,self.the_lowest_temperatrue =self.get_tomorrow_weather_and_temperature()
self.content =self.email_content()
deftearDown(self):
self.dr.quit()
defget_tomorrow_weather_and_temperature(self):
self.baidu_search(u'深圳天气')
sleep(5)
weather =self.dr.find_elements_by_css_selector('.op_weather4_twoicon_weath')[1].text#首先获取明日天气
print(u'明日天气为%s'% weather)
temperature =self.dr.find_elements_by_css_selector('.op_weather4_twoicon_temp')[1].text#其次获取明日温度
the_highest_temperature =int(temperature.split(' ~ ')[0])#从温度中获取明日最高温度并转为整型
print(u'明日最高温度%s℃'% the_highest_temperature)
the_lowest_temperature = temperature.split(' ~ ')[1]#从温度中获取明日最低温度
print(u'明日最低温度%s'% the_lowest_temperature)
returnweather,the_highest_temperature,the_lowest_temperature
defbaidu_search(self,CityWeather):
self.dr.maximize_window()
self.dr.get('http://www.baidu.com')
self.dr.find_element_by_id('kw').send_keys(CityWeather)
self.dr.find_element_by_id('su').click()
defemail_content(self):
ifu'雨'inself.weather:
content =u'通知:明天有雨,请全体同事带伞!'
else:
ifself.the_highest_temperature >30:
content =u'提醒:明日气温高于30度,请全体同事注意高温'
elifself.the_highest_temperature <10:
content =u'提醒:明日气温低于10度,请全体同事注意保暖'
else:
content =u'放心上班,开心上班'
returncontent
deftest_send_email1(self):
print('%s'%self.content)
if__name__ =='__main__':
unittest.main()
收藏
举报
TAG:
Python
python
编程