从百度天气获取当地明天的天气情况,如果明天下雨,请发送邮件通知全体同事带伞,

如果明天气温低于10度,请邮件提醒同事注意保暖,如果气温高于30度则提醒同事注意高温。

假设存在发送邮件的方法self.send_email(email_content)

代码如下:

#coding=utf-8
from selenium import webdriver
import unittest
from time import sleep

class WeatherReport(unittest.TestCase):

    def setUp(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()

    def get_tomorrow_weather_and_temperature(self):
        self.baidu_search('成都天气')
        sleep(5)
        weather = self.dr.find_elements_by_css_selector('.op_weather4_twoicon_weath')[1].text #首先获取明日天气
        print('明日天气为%s' %weather)
        temperature = self.dr.find_elements_by_css_selector('.op_weather4_twoicon_temp')[1].text #其次获取明日温度
        the_highest_temperature = int(temperature.split(' ~ ')[0]) #从温度中获取明日最高温度并转为整型
        print('明日最高温度%s℃' %the_highest_temperature)
        the_lowest_temperature = temperature.split(' ~ ')[1] #从温度中获取明日最低温度
        print('明日最低温度%s' %the_lowest_temperature)
        return weather, the_highest_temperature, the_lowest_temperature

    def baidu_search(self, CityWeather):
        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()

    def email_content(self):
        if '雨' in self.weather:
            content = '通知:明天有雨,请全体同事带伞!'
        else:
            if self.the_highest_temperature > 30:
                content = '提醒:明日气温高于30度,请全体同事注意高温'
            elif self.the_highest_temperature < 10:
                content = '提醒:明日气温低于10度,请全体同事注意保暖'
            else:
                content = ''
        return content

    def test_send_email(self):
        print('%s' %self.content)

    def tearDown(self):
        self.dr.quit()

if __name__ == '__main__':
    unittest.main()

网页如下:

实现如下:

用python+selenium从百度获取本地明日的天气信息并根据温度情况设置提醒的更多相关文章

  1. python selenium 练习 自动获取豆瓣阅读当前特价书籍 chrome 元素定位 窗口切换 元素过期

    豆瓣原创电子书每周推出数十本限时免费数目,一周免费期过后恢复原价.想着豆瓣原创书中有不少值得一看,便写了个脚本,免去一个个添加的烦恼. 使用了Windows下selenium+Python的组合,有较 ...

  2. python+selenium实现百度关键词搜索自动化操作

    缘起 之前公司找外面网络公司做某些业务相关关键词排名,了解了一下相关的情况,网络公司只需要我们提供网站地址和需要做的关键词即可,故猜想他们采取的方式应该是通过模拟用户搜索提升网站权重进而提升排名. 不 ...

  3. Python + Selenium 练习篇 - 获取页面所有邮箱

    代码如下: # coding=utf-8import re    #python中利用正则,需要导入re模块from selenium import webdriverdriver = webdriv ...

  4. python selenium实现百度搜索

    1.环境 python2.7+selenium+phantomjs+linux 2.代码 #-*-coding:utf-8 -*- from selenium import webdriver fro ...

  5. Python使用WMI模块获取Windows系统的硬件信息,并使用pyinstaller库编译打包成exe的可执行文件

    由于公司现阶段大多数应用软件都是基于Windows系统开发和部署,很多软件安装部署都是在windows server 2012.windows server 2008之类的服务器上,部门同事每次测试一 ...

  6. Python+selenium爬取智联招聘的职位信息

    整个爬虫是基于selenium和Python来运行的,运行需要的包 mysql,matplotlib,selenium 需要安装selenium火狐浏览器驱动,百度的搜寻. 整个爬虫是模块化组织的,不 ...

  7. [VC]获取本地程序的版本信息信息

    CString CQwerApp::IS_GetAppVersion(char *AppName) { ////需要加上version.lib在link里 CString AppVersion; // ...

  8. python使用wmi模块获取windows下的系统信息监控系统-乾颐堂

    Python用WMI模块获取Windows系统的硬件信息:硬盘分区.使用情况,内存大小,CPU型号,当前运行的进程,自启动程序及位置,系统的版本等信息. 本文实例讲述了python使用wmi模块获取w ...

  9. Linux下编程获取本地IP地址的常见方法

    转载于:http://blog.csdn.net/k346k346/article/details/48231933 在进行linux网络编程时,经常用到本机IP地址.本文罗列一下常见方法,以备不时之 ...

随机推荐

  1. blade and soul races guide

    Race Four races are available for those who wish to choose the path of martial arts: the careful Gon ...

  2. Strategy pattern策略模式

    在Java的集合框架中,经常需要通过构造方法传入一个比较器Comparator,或者创建比较器传入Collections的静态方法中作为方法参数,进行比较排序等,使用的是策略模式. 一.策略模式的定义 ...

  3. hdoj 1576

    //1Y真是爽啊 题意:要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1).  分析:根据题意a=b*x   a=m ...

  4. python学习04——列表的操作

    笨办法学python第38节 如何创建列表在第32节,形式如下: 本节主要是讲对列表的操作,首先讲了 mystuff.append('hello') 的工作原理,我的理解是,首先Python找到mys ...

  5. JAVA 中文转GBK内码方法

    不能谷歌,百度了很久,没有直接的转换方法,参考 byte[]数组与十六进制字符串与字符串的互相转换 http://blog.163.com/roadwalker@126/blog/static/113 ...

  6. CONTAINING_RECORD的实现

    // 9.25.cpp : 定义控制台应用程序的入口点.///*----------------------------CONTAINING_RECODER的实现------------------- ...

  7. hive问题整理(待续)

    本人对hadoop生态系统的环境搭建.配置相关再熟悉不过了,周末刚测试过oozie相关的 今早使用hive,报错: Exception in thread "main" java. ...

  8. VMware下利用ubuntu13.04建立嵌入式开发环境之五

    tftp和nfs服务器配置 一.tftp服务器配置 1.安装软件包 1.1安装服务 apt-get install tftpd-hpa 1.2安装客户端 apt-get install tftp-hp ...

  9. Ext JS 4 新特性2:配置项属性(config)之二

    Ext JS 4 新特征2:配置项属性config之二 ☞ Config(自动的setters和getters) Ext JS 4介绍了config声明方式,在Ext JS 中也有几个例子:在运行程序 ...

  10. Java IO流

    File类 ·java.io.File类:文件和目录路径名的抽象表示形式,与平台无关 ·File能新建.删除.重命名文件和目录,但File不能访问文件内容本身.如果需要访问文件内容本身,则需要使用输入 ...