【Python】博客信息爬取-微信消息自动发送
2023-09-11 14:17:09 时间
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脚本扫描给定网段的MAC地址表(scapy或 python-nmap)
- python加密提示ModuleNotFoundError: No module named 'Crypto'
- 【Python】ValueError: unsupported pickle protocol: 5解决方案/同一个项目 python环境尽量保持一致
- Python实现FTP服务器和客户端
- 【华为OD机试真题 python】银行插队【2023 Q1 | 100分】
- hive使用python脚本导致java.io.IOException: Broken pipe异常退出
- 零基础教你快速入门Python怎么学python入门?python新手学习路线
- 24岁非计算机专业工科妹子裸辞转行Python程序员,自学Python三个月,零基础如何系统学习python,从入门到精通?
- 【Python】自动化抢勾,python软件安装教程
- 2023版python安装教程奉上,Python永久使用 超详细版,一看就会【小白友好】
- python中functools.cache用法详解及缓存策略问题
- Python中的函数编写示例代码
- Python接口自动化核心模块 - 数据库操作和日志
- 利用python+flask实现股票查询与结果网页展示
- Python大婶博客汇总
- 《Python算法教程》——1.7 练习题
- python动态规划算法的使用过程
- Python 机器学习之使用 AUTO ARIMA + PROPHET + LightGBM 进行时间序列预测
- 【Python】python扩展
- Python 工具 之 Windows 上 python 虚拟环境的搭建与简单使用的相关说明
- 【Python注意事项】如何理解python中间generator functions和yield表情
- Python用python-docx读写word文档
- 《剑指offer》面试题16:从上往下打印二叉树(Python)
- Selenium4 Python实现Page Factory设计模式,python新的定位方法