码迷,mamicode.com
首页 > 微信 > 详细

【Python】博客信息爬取-微信消息自动发送

时间:2019-04-05 00:30:06      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:报告   调用   string   pytho   inux   ade   发送   rip   null   

1、环境安装

python -m pip install --upgrade pip

pip install bs4

pip install wxpy

pip install lxml

2、博客爬取及发送

 

from bs4 import BeautifulSoup
from threading import Timer
import requests
import traceback
from wxpy import *


url = ‘‘

nIndex = 6
my_groups = None

def getMsg(nIndex):
    # 获得网址源码
    html = requests.get(url).content
    soup = BeautifulSoup(html, lxml) 

    # print(‘开始抓取‘)
    # title = soup.title.string
    # print(title)

    # areaall = soup.find(id=‘sina_keyword_ad_area2‘).children
    areaall = soup.select(#sina_keyword_ad_area2 p)
    # print(areaall)
    iCount = len(areaall)
    # print(iCount)

    # print(nIndex)
    msg = ""
    if iCount < nIndex:
        return msg,0
    else:        
        msg = areaall[iCount - 1]
        msg = msg.get_text()
        # print(msg)
        return msg,iCount

msgTemp = ‘‘
nNullMsg = 0

def auto_send():
    try:
        global nIndex
        global my_groups
        global msgTemp
        global nNullMsg
        msgContent,nIndexMsg = getMsg(nIndex)
        msgContent = str(msgContent).strip()
        # print(nIndexMsg)
        # nIndex += 1
        # print(msgContent)
        if len(msgContent) != 0 :
            # print(str(nIndex) + ":\t" + msgContent)
            # return 
            if msgContent != msgTemp :              
                if my_groups != None and len(my_groups) > 0 :
                    print("发送消息:" + msgContent)
                    my_groups[0].send(msgContent)
                    msgTemp = msgContent
            else:
                print(消息已发送)
        else:
            nNullMsg += 1
            print("没有新消息")
            if nNullMsg == 20 :
                print("恭喜发财,今日推送完毕")
                return

        
        # # 每隔86400秒(1天),发送1次
        t = Timer(3, auto_send)
        t.start()

    except  Exception as e:
        print(e)
        # 你的微信昵称,注意这里不是备注,也不是微信帐号
        my_friend = bot.friends().search(NetUML)[0]
        my_friend.send(u"报告老板,今日份的信息发送失败了!")



if __name__ == "__main__":


    # 初始化机器人,扫码登陆微信,适用于Windows系统
    # 初始化一个机器人对象
    bot = Bot(cache_path=True)

    my_groups = bot.groups().search(广告技术)    

    for group in my_groups:
        print(group)   

    # # Linux系统,执行登陆请调用下面的这句
    # bot = Bot(console_qr=2, cache_path="botoo.pkl")

    # 调用函数进行消息发送
    auto_send()

 

【Python】博客信息爬取-微信消息自动发送

标签:报告   调用   string   pytho   inux   ade   发送   rip   null   

原文地址:https://www.cnblogs.com/defineconst/p/10657504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!