微信机器人自动发送游戏资讯top10到微信群和好友

python 阅读:541

今天有空写了个微信机器人发送游戏资讯信息的脚本,正好也是最近的一个需求。

脚本可以在windows,Mac,Linux下运行:

#coding:utf-8
# @Time    : 2018/9/15 13:42
# @Author  : zbb


import time,datetime
import xlrd
import json
import platform
from lxml import etree
import requests
from wxpy import *
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


class Game_Info(object):
    def __init__(self):
        self.top10 = {}
        self.sysstr = platform.system()
        if self.sysstr == "Windows":
            self.bot = Bot(cache_path=True)
        elif self.sysstr == "Linux":
            self.bot = Bot(cache_path=True,console_qr=True)
        else:
            self.bot = Bot(cache_path=True)

    def get_web_info(self,url_title,url,url_href,url_text):
        """
        :param url_title: 网站的名称
        :param url: 网站的链接
        :param url_href: 爬取网站的href表达式
        :param url_text: 爬取网站的标题表达式
        :return:
        """
        r = requests.get(url)
        html = r.content
        selector = etree.HTML(html)
        href_contents = selector.xpath(url_href)
        text_contents = selector.xpath(url_text)
        info = []
        for index, c in enumerate(href_contents):
            info.append({"title": text_contents[index], "link": c})
        self.top10[url_title] = info

    def get_game_top(self):
        """
        微信发送内容
        :return:
        """
        top10_info = "{0}月{1}日 游戏top10 \n\n".format(time.strftime("%m"),time.strftime("%d"))
        for k, v in self.top10.items():
            single_title = "【{0}】\n".format(k)
            for i in v:
                single_title += i["title"].strip() +"\n" +i["link"] + " \n"
            single_title += "-------------------\n\n"
            top10_info += single_title
        return top10_info

    def wx_group_send(self,msg,group_name):
        """
        
        :param msg: 微信消息内容
        :param group_name: 微信群名称
        :return: 
        """
        my_group = self.bot.groups().search(u"{0}".format(group_name))[0]
        print my_group
        my_group.send(msg)
    def wx_friend_send(self,msg,friend_name):
        """
        
        :param msg: 微信消息内容
        :param friend_name: 微信好友名称
        :return: 
        """
        my_friend = self.bot.friends().search(name = u'{0}'.format(friend_name))[0]
        print my_friend
        my_friend.send(msg)

if __name__ == "__main__":
    crawlers = [
        ("手游那点事","http://www.nadianshi.com/",'//div[@class="partCont_todayFocus"]/ul/li/a/@href','//div[@class="partCont_todayFocus"]/ul/li/a/text()'),
        ("游戏大观网", "http://www.gamelook.com.cn/",'//div[@class="home-section-center"]/ul/li/div[@class="item-content"]/h2/a/@href','//div[@class="home-section-center"]/ul/li/div[@class="item-content"]/h2/a/text()'),

    ]
    gi = Game_Info()
    for url_info in crawlers:
        url_title, url, url_href, url_text = url_info
        gi.get_web_info(url_title, url, url_href, url_text)
    msg = gi.get_game_top()
    # gi.wx_friend_send(msg,friend_name)
    group_name = "your group_name"
    gi.wx_group_send(msg,group_name)

将自己微信中存在的微信群名称赋值给group_name,运行程序,会显示一个二维码,用自己的手机微信扫描二维码后就可以发送游戏资讯到指定的微信群了。想要发送资讯给好友设置friend_name就可以了。效果如下:

image.png



参考文献:https://wxpy.readthedocs.io/zh/latest/