python定时发送,天气,文本信息,发送给指定好友、群。

import requests
from requests import exceptions
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
from wxpy import *
import  schedule
import  time
 '''
遇到不懂的问题?Python学习交流群:821460695满足你的需求,资料都已经上传群文件,可以自行下载!
'''
 
#bot=Bot(cache_path=True) #登陆网页微信,并保存登陆状态
bot = Bot(console_qr=2,cache_path="botoo.pkl")#Linux专用,像素二维码
 
def sendblogmsg(content):
    #搜索自己的好友,注意中文字符前需要+u
    my_friend = bot.friends().search(u'王琳杰')[0]
    my_friend.send(content)

    my1_friend = bot.friends().search(u'浮生若梦')[0]
    my1_friend.send(content)

    my_group = bot.groups().search(u'聊天机器人测试')[0]
    my_group.send(content) #发送天气预报



    my1_group = bot.groups().search(u'测试')[0]
    my1_group.send(content) #发送天气预报
 
def job():
    resp=urlopen('http://www.weather.com.cn/weather/101010100.shtml')
    soup=BeautifulSoup(resp,'html.parser')
    tagToday=soup.find('p',class_="tem")  #第一个包含class="tem"的p标签即为存放今天天气数据的标签
    try:
        temperatureHigh=tagToday.span.string  #有时候这个最高温度是不显示的,此时利用第二天的最高温度代替。
    except AttributeError as e:
        temperatureHigh=tagToday.find_next('p',class_="tem").span.string  #获取第二天的最高温度代替
 
    temperatureLow=tagToday.i.string  #获取最低温度
    weather=soup.find('p',class_="wea").string #获取天气
    contents = '北京' + '\n' +  '最高温度:' + temperatureHigh + '\n' + '最低温度:' + temperatureLow + '\n' +  '天气:' + weather 
       # result3 = '最低温度:' + temperatureLow
    #print('最低温度:' + temperatureLow)
    #print('最高温度:' + temperatureHigh)
   # print('天气:' + weather)
    sendblogmsg(contents)


def sendblogmsg_1(content):
    #搜索自己的好友,注意中文字符前需要+u
    my_friend = bot.friends().search(u'王琳杰')[0]
    my_friend.send(content)

def job_1():

    if bot == None:
        login_wechat()

    contents = '早点休息,晚安?' 
       # result3 = '最低温度:' + temperatureLow
    #print('最低温度:' + temperatureLow)
    #print('最高温度:' + temperatureHigh)
   # print('天气:' + weather)
    sendblogmsg_1(contents)




#定时
schedule.every().day.at("22:50").do(job) #规定每天12:30执行job()函数
schedule.every().day.at("22:50").do(job_1) #规定每天12:30执行job()函数
while True:
    schedule.run_pending()#确保schedule一直运行
    time.sleep(1)
bot.join() #保证上述代码持续运行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值