• 每天自动给自己发天气预报的脚本


    需求:

    每天早上起来可以看看天气预报,然后顺便当个闹钟使

    思路是这样的:

    模块一:采用yahoo weather api获取北京的天气

    模块二:通过网页版飞信,模拟飞信登陆,给自己发短信

    模块三:发送信息

    一。get_yahoo_weather.py

    #!/usr/bin/env python
    #coding=utf-8
    import urllib2
    from xml.etree import cElementTree as ET
    class GetWeather:
        def __init__(self):
            self.weather = self.makexml()
        def makexml(self):
            url = "http://weather.yahooapis.com/forecastrss?w=2151330&u=c"
            res = urllib2.urlopen(url)
            xmlfile = open("yahoo.xml",'w')
            xmlfile.writelines(res.read())
            xmlfile.close()
            return self.xmlET()
    
        def xmlET(self):
            tree = ET.ElementTree(file="yahoo.xml")
            forecast = []
            for elem in tree.iter(tag="{http://xml.weather.yahoo.com/ns/rss/1.0}forecast"):
                forecast.append(elem.attrib)
            return self.msg(forecast)
    
        def msg(self,forecast):
            msg_data = ""
            fmt = "
    %s              %s    %s   %s
    "%("日期","天气","最高温","最低温")
            msg_data += fmt
            for i in forecast:
                msg_data += "%s        %s    %s    %s"%(i['date'],i['text'],i['high'],i['low'])
                msg_data +="
    "
            return msg_data
    
    if __name__ == "__main__":
        w = GetWeather()
        print w.weather

    二。fetionsimu.py

    #!/usr/bin/env python
    #coding=utf-8
    
    import cookielib
    import urllib
    import urllib2
    import json
    import re
    import time
    
    def fetion(weather_msg = "no data"):
        cj = cookielib.LWPCookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        urllib2.install_opener(opener)
        t = time.localtime()
        print t.tm_year,t.tm_mon,t.tm_mday
        print "logining"
        urlbase = "http://f.10086.cn/im5/" 
        response_text = urllib2.urlopen(urlbase)
        response_text = response_text.read()
        partten = re.compile(r'login/login.action?mnative=d&t=d+')
        urlplus = partten.search(response_text)
        urlcomplete = urlbase + urlplus.group(0)
    #    print urlcomplete
        qheader = {'Referer':urlcomplete}
        args = {'m':'your phone number','pass':'your password'}
        logurl = "http://f.10086.cn/im5/login/loginHtml5.action"
        req = urllib2.Request(logurl,urllib.urlencode(args),qheader)
        jump = opener.open(req)
        page = jump.read()
    #    print page
        page = json.loads(page)
        if page['nickname'] == "LGY":
            print "login successfully"
        else:
            print "error in pass or username"
        sendmsgurl = "http://f.10086.cn/im5/chat/sendNewGroupShortMsg.action"
        msg_data = {"touserid":page["idUser"],"msg":weather_msg}
        msg_back = urllib2.Request(sendmsgurl,urllib.urlencode(msg_data),qheader)
        msg_jump = opener.open(msg_back)
        msguse = msg_jump.read()
        msguse = json.loads(msguse)
        if msguse["info"] == u"发送成功":
            print "send successfully"
        else:
            print msguse
            print "send failed"
    if __name__ == "__main__":
        fetion()

    三。sendweather.py

    #!/usr/bin/env python
    
    import time
    from get_yahoo_weather import GetWeather
    from fetionsimu import fetion
    def sendmsg(w):
        fetion(w)
    
    if __name__ == "__main__":
        w = GetWeather()
        sendmsg(w.weather)

    最后加入开机启动

    用crontab -e编辑开机启动项

    0 7 * * * /test/mkfun/sendweather.py > /test/mkfun/sdwthmsg.log 2>&1

    然后就ok了。。。

  • 相关阅读:
    windows常用命令
    Qt 添加 QtNetwork 库文件
    LoadLibrary加载动态库失败
    C++11 Function 使用场景
    编程书籍集
    代码重构例集
    多重循环编码规范
    vim 命令学习(基础篇)
    QT构建窗体(父窗体传为野指针)异常案例
    JAVA_SE基础——26.[深入解析]局部变量与成员变量的差别
  • 原文地址:https://www.cnblogs.com/lgy6534588/p/3459120.html
Copyright © 2020-2023  润新知