zoukankan      html  css  js  c++  java
  • python 简单爬虫获取气象数据发送气象定时报-预报预警信息及时推送及阿里云短信群发接口

    !/usr/bin/python
    #encoding=utf-8 
    #Author:Ruiy
    
    #////////////////////////////////////////////////////
    #python-bs4
    #ilio
    #///////////////////////////////////////////////////
    
    import sys
    import datetime
    nowTime=""" + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + """
    #print nowTime
    addr=""长沙""
    
    
    if sys.getdefaultencoding() != 'utf-8':
        reload(sys)
    sys.setdefaultencoding('utf-8')
    
    #apt-get install python-bs4 for Ubuntu
    
    
    from urllib import urlopen
    from bs4 import BeautifulSoup
    import re
    
    url='http://www.weather.com.cn/weather/101250101.shtml'
    html=urlopen(url).read()
    soup=BeautifulSoup(html,'html.parser',from_encoding='utf-8')
    
    #print soup.prettify()
    #print soup.title
    
    #tagAfterTomorrow=soup.find('li',class_="sky skyid lv3")
    #print tagAfterTomorrow
    
    
    #weather=tagAfterTomorrow.find_next('p',class_="wea").string
    #print weather
    
    #temperatureHigh=tagAfterTomorrow.find_next('span').string
    #print temperatureHigh
    
    #temperatureLow=tagAfterTomorrow.find_next('i').string
    #print temperatureLow
    
    #windDirection=tagAfterTomorrow.find_next('span',class_='SE').string
    #print windDirection
    
    #res_data=soup.findALL('script')
    #print res_data
    
    res_data=soup.find('ul','t clearfix')
    #print res_data
    
    #print twoDays
    
    #h1s=soup.find_all('h1')
    #for h1 in h1s:
    #    for li in h1.children:
    #            print h1.string
    
    li_tag=res_data.findAll('li')
    #print li_tag
    #print 'bug/////////////////////////'
    
    #for i in li_tag:
    #    print i
    
    #print "/////////ruiy"
    
    #数据回值
    for tag in li_tag:
        patchTwoDays=tag.find('h1').string
        if '后天' in patchTwoDays:
            TC=tag.find('h1').string #时间
            T=""" + TC + """
            print T
            WC=tag.find('p','wea').string #天气
            W=""" + WC + """
            print W
            try:
                HC=tag.find('p','tem').find('span').string #最高温
                H=""" + HC + """
                print H
                LC=tag.find('p','tem').find('i').string #最低温度
                L=""" + LC + """
                print L
            except:
                #print("没有高温低温数据")
                pass
            FXC=tag.find('p','win').find('i').string #风向级数
            FX=""" + FXC + """
            print FX
            #print  "patch"
            #print(tag.find('h1').string)  #时间
            #print(tag.find('p','wea').string) #天气
            #try:
            #    print(tag.find('p','tem').find('span').string) #最高温
            #    print(tag.find('p','tem').find('i').string) #最低温
            #except:
            #    print("没有高温低温数据")
            #    pass
            #print(tag.find('p','win').find('i').string) #风向级数
            #print('_______________ 分割线 ____________________')
        #else:
        #    print 'no patch'
    
    
    #Email mess interface
    #import smtplib
    #from email.mime.text import MIMEText
    #from email.utils import formataddr
    #import time
    
    #_user="5160558@qq.com"
    #_pwd="nphqgzgcbce"
    #_to="1507@139.com"
    
    #receives=[
    #    "5160@qq.com",
    #    "1505567****@139.com",
    #    "374093@qq.com",
    #]
    
    #for i in receives:
    #    msg=MIMEText("te")
    #    msg["Subject"]="dot't p"
    #    msg["Form"]=_user
    #    msg["To"]=i
    #    try:
    #        s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    #        s.login(_user, _pwd)
    #        s.sendmail(_user, i, msg.as_string())
    #        s.quit()
    #        print("Success!")
    #    except smtplib.SMTPException,e: 
    #        print ("Falied,%s" %e) 
    
    #阿里短信接口
    import sys
    from aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest
    from aliyunsdkdysmsapi.request.v20170525 import QuerySendDetailsRequest
    from aliyunsdkcore.client import AcsClient
    import uuid
    from aliyunsdkcore.profile import region_provider
    from aliyunsdkcore.http import method_type as MT
    from aliyunsdkcore.http import format_type as FT
    import const
    
    """
    短信业务调用接口
    
    """
    try:
        reload(sys)
        sys.setdefaultencoding('utf8')
    except NameError:
        pass
    except Exception as err:
        raise err
    
    # 注意:不要更改
    REGION = "cn-hangzhou"
    PRODUCT_NAME = "Dysmsapi"
    DOMAIN = "dysmsapi.aliyuncs.com"
    
    acs_client = AcsClient(const.ACCESS_KEY_ID, const.ACCESS_KEY_SECRET, REGION)
    region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN)
    
    
    def send_sms(business_id, phone_numbers, sign_name, template_code, template_param=None):
        smsRequest = SendSmsRequest.SendSmsRequest()
        # 申请的短信模板编码,必填
        smsRequest.set_TemplateCode(template_code)
    
        # 短信模板变量参数
        if template_param is not None:
            smsRequest.set_TemplateParam(template_param)
    
        # 设置业务请求流水号,必填。
        smsRequest.set_OutId(business_id)
    
        # 短信签名
        smsRequest.set_SignName(sign_name)
    
        # 数据提交方式
            # smsRequest.set_method(MT.POST)
    
            # 数据提交格式
        # smsRequest.set_accept_format(FT.JSON)
    
        # 短信发送的号码列表,必填。
        smsRequest.set_PhoneNumbers(phone_numbers)
    
        # 调用短信发送接口,返回json
        smsResponse = acs_client.do_action_with_exception(smsRequest)
    
        # TODO 业务处理
    
        return smsResponse
    
    
    if __name__ == '__main__':
        __business_id = uuid.uuid1()
        #plist=['13121172','15198367']
        pce=['1312272***','15055367****','15316']
        #print(__business_id)
        #params = "{"code":"12345","product":"云通信"}"
        #params = {"nowTime":nowTime,"addr":"长沙","T":"12345","W":"12345","H":"12345","L":"12345","FX":"12345"}"
        #params = {"nowTime":nowTime,"addr":addr,"T":T,"W":W,"H":H,"L":L,"FX":FX}
        #params = "{"nowTime":%s,"addr":"云通信","T":"12345","W":"12345","H":"12345","L":"12345","FX":"12345",%nowTime}"
        #params="{"nowTime":%s,"addr":"云通信","T":"12345","W":"12345","H":"12345","L":"12345","FX":"12345"}",%nowTime
        pms = """{"nowTime":%(nowTime01)s,"addr":%(addr01)s,"T":%(T01)s,"W":%(W01)s,"H":%(H01)s,"L":%(L01)s,"FX":%(FX01)s}"""
        params = pms % dict(nowTime01=nowTime,addr01=addr,T01=T,W01=W,H01=H,L01=L,FX01=FX)
        #print paramsop
        #params = "{"nowTime":"12345","addr":"云通信","T":"12345","W":"12345","H":"12345","L":"12345","FX":"12345"}"
        #print params
        #params = "{"nowTime":"12345","addr":"云通信","T":"12345","W":"12345","H":"12345","L":"12345","FX":"12345"}"
        for i in pce:
            #params = u'{"name":"wqb","code":"12345678","address":"bz","phone":"13000000000"}'
            print(send_sms(__business_id,i, "位温", "SMS_1297439", params))

    //预警数据采集,未完..

    #!/usr/bin/python
    #encoding=utf-8
    #Author:Ruiy
    
    #////////////////////////////////////////////////////
    #python-bs4
    #ilio
    #///////////////////////////////////////////////////
    
    import sys
    import datetime
    
    
    if sys.getdefaultencoding() != 'utf-8':
            reload(sys)
    sys.setdefaultencoding('utf-8')
    
    #apt-get install python-bs4 for Ubuntu
    
    
    from urllib import urlopen
    from bs4 import BeautifulSoup
    import re
    
    url='http://www.hnqx.gov.cn/yjs.asp'
    html=urlopen(url).read()
    soup=BeautifulSoup(html,'html.parser',from_encoding='utf-8')
    
    res_data=soup.find_all('td')
    print res_data

     阿里短信推送测试,因为只是测试推送的数据爬取间隔很短,正常使用预报是定时报,预警的功能实现中,预警的是有预警立即推送信息!!@

     邮件推送测试结果

     

  • 相关阅读:
    洛谷 1443——马的遍历(广度优先搜索)
    jzoj C组 2017.1.21
    jzoj C组 2017.1.20 比赛
    jzoj C组 2017.1.19 比赛
    jzoj C组 2017.1.18 比赛
    jzoj C组 2017.1.17 比赛
    jzoj C组 2017.1.16 比赛
    jzoj C组 2017.1.15比赛
    [LCA][数学]JZOJ 4794 富爷说是一棵树
    [CDQ分治][带权并查集]JZOJ 4769
  • 原文地址:https://www.cnblogs.com/ruiy/p/8711347.html
Copyright © 2011-2022 走看看