wxpy

测试,尚未开发完成

from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests
from lxml import etree
# 登录微信
bot = Bot()


def send_news():
    try:
        # 好友的微信号,可以在获取好友列表中找
        my_friend = bot.friends().search(u'爸爸')[0]
        data = get_weather()
        # my_friend.send(u"t")  # 描述自己修改
        my_friend.send(data)  # 描述自己修改
        t = Timer(1000, send_news)  # 设置发送时间间隔
        t.start()
    except:
        my_friend = bot.friends().search('梧桐')[0]   # 自己的微信号
        my_friend.send(u"今天消息发送失败了")    # 描述自己修改


def get_weather():
    # 要改为自己想要获取的城市的url,下面是青岛的url
    response = requests.get('http://www.weather.com.cn/weather1d/101190801.shtml')
    selector = etree.HTML(response.content.decode('utf-8'))
    # 温度
    height_temp = selector.xpath('//*[@id="today"]/div[2]/ul/li[1]/p[2]/span//text()')[0]
    lower_temp = selector.xpath('//*[@id="today"]/div[2]/ul/li[2]/p[2]/span/text()')[0]
    temp = "{}°C ~ {}°C".format(lower_temp, height_temp)
    # 日出 日落时间
    sun_up = selector.xpath('//*[@id="today"]/div[2]/ul/li[1]/p[4]/span//text()')[0]
    sun_down = selector.xpath('//*[@id="today"]/div[2]/ul/li[2]/p[4]/span//text()')[0]
    sun = "时间:{} {}".format(sun_up, sun_down)
    # 天气
    weather = selector.xpath('//*[@id="today"]/div[2]/ul/li[1]/p[1]/text()')[0]
    # 风向
    win_direction = selector.xpath('//*[@id="today"]/div[2]/ul/li[1]/p[3]/span/@title')[0]
    win_power = selector.xpath('//*[@id="today"]/div[2]/ul/li[1]/p[3]/span/text()')[0]
    win = "{} {}".format(win_direction, win_power)
    weather_content = '徐州今日:' + '\n' + '天气:' + weather + '\n' + '温度:' + temp + '\n' \
                      + '风力:' + win + '\n' + sun + '\n' + '注意天气变化!!'
    return weather_content
    # print(a)
    # print(sun)
    # print(b)
# def sendblogmsg(content):
#     # 搜索自己的好友,注意中文字符前需要+u
#     my_group = bot.friends().search(u'**你要发送的对象微信备注名**')[0]
#     my_group.send(content)  # 发送天气预报


if __name__ == "__main__":
    send_news()
    # data = get_weather()
    # print(data)

 

posted @ 2020-04-03 17:22  市丸银  阅读(354)  评论(0编辑  收藏  举报