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

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()
上一篇:Java数组运算


下一篇:Python大婶博客汇总