利用twilio做一个天气预报短信系统

@Ta 2019-01-06发布,2019-01-07修改 4656点击
新手写的勿喷!
需要pip install twilio
还要注册twilio帐号,发信息是免费的。但是要绑定手机号码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
####By晓晓谦###
import urllib.request,re,time
from twilio.rest import Client
#########天气爬虫部分#######
url = "http://m.weather.com.cn/d/town/index?lat=29.633181&lon=111.758682"
######手机访问http://m.weather.com.cn/给地理位置权限后复制网址替换。
########或手动替换经纬度
#####如深圳龙岗区经纬度为:22.726017,114.254454
#####则修改为http://m.weather.com.cn/d/town/index?lat=22.726017&lon=114.254454
#####地理位置经纬度查询http://gpsspg.com/maps.htm
html= urllib.request.urlopen(url).read()
html = html.decode('utf-8')
dq= re.findall('<h2> (.+?)市<span>|</span> (.+?)<img src="http://i.tq121.com.cn/i/wap2017/xl.png" ',html)
dq = ''.join(dq[0])+'市'+''.join(dq[1])  ####地区
wd= re.findall('><span>(.+?)</span><b>°</b><em> ',html)
wd = '⭐温度:'+(" ".join(wd))+'℃'+'\n'
    ####温度
fx  = re.findall('flfx iconli">(.+?)\s+?	(.+?)</span>',html)  
fx='⭐风向:'+ " ".join(fx[0])+'\n' ###风向
sd = re.findall('<span class="xdsd iconli">(.+?)</span>',html)
sd  = '⭐湿度:'+" ".join(sd)+'\n'    ####湿度
zw = re.findall('紫外线<b>(.+?)\s+?<b style="display:none"',html)
zw = '⭐紫外线强度:'+(" ".join(zw))+'\n'
   ####紫外线
zxs1 = re.findall('class="timeLi">(.+?)</div><i\sclass="svnicon housr_icons .+?"></i><div\sclass="tempLi">(.+?)</div></div>\s',html)
zxs = '⭐最近9小时温度:\n⭐'+''.join(zxs1[0])+'|'+''.join(zxs1[1])+'|'+''.join(zxs1[2])+'\n⭐'+''.join(zxs1[3])+'|'+''.join(zxs1[4])+'|'+''.join(zxs1[5])+'\n⭐'+''.join(zxs1[6])+'|'+''.join(zxs1[7])+'|'+''.join(zxs1[8])+'\n'     ####最近9小时温度

tq = wd+fx+sd+zw+zxs
##########发短信部分###########
#######需要用到twilio模块和注册twilio帐号
######注册地址http://www.twilio.com/
account_sid = 'AC*****************j*'####
twilio的sid
auth_token = '2f*******89b8********3'
####twilio的token
client = Client(account_sid, auth_token)
text = '----------------\n⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐\n⭐晓谦'+dq+'天气系统\n'+tq+'⭐现在是北京时间:\n⭐'+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+'\n⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐\n-----------------------------'
message = client.messages.create(
                              from_='+15673013653',
####twilio给的发信人手机号码
                              body=text,
                              to='+86'
######收信人手机号码需要在twilio上绑定
                          )

print('短信输出:\n',message.sid)
print('天气输出:\n',tq)
print('城市:',dq)

效果图
f4aff5406eb19363119b577234f11954391572.jpg
回复列表(4|隐藏机器人聊天)
添加新回复
回复需要登录