利用 wxpy 进行微信信息自动发送, 简直是骚扰神器, 除非拉黑你.
那我们就来设置一个场景吧, 五秒发送一次, 一次发送 10 条
首先我们来整理一下思路:
1. 首先我们要从网上爬去我们想要发送的内容
2. 登录微信找到想要发送的微信好友
3. 讲爬取的信息发送给好友
首先我们需要的模块:
- import requests
- import wxpy,threading,time
- from bs4 import BeautifulSoup
代码草稿及调试如下:
- import wxpy,threading,requests,time# 如果报错: AttributeError: module 'wxpy' has no attribute 'Bot'
- # 一定要检查是不是文件夹名字或者文件名字和你要用模块是不是一致了不能一样
- from bs4 import BeautifulSoup
- def get_next(nub):
- '''获取要发送的内容'''
- url = 'http://www.59xihuan.cn/index_'# 我们观察到每一页只是和上一页的 url 中一个数据不一样而且是有规律性的递增
- response = requests.get(url= url + str(nub) + '.html')
- a = BeautifulSoup(response.text,'html.parser')# 进行解析
- txte1 = a.find_all('div',class_='pic_text1')# 找到需要的内容的那个标签所在的行
- #print(txte1)
- new_txte = []
- for temp in txte1:
- new_txtes = temp.text# 从经验上讲从一般的经验上讲: :string 被用于短文本输入(用户名, 电子邮件, 密码, 标题等).
- #:text 被用于预计文本长度较长的文本输入(描述, 评论内容等).
- news_text = new_txtes.replace('\r\n \r\n', '')
- news_text = news_text.replace('\r\n \n', '')# 去掉前后空格
- new_txte.append(news_text)# 从经验上讲从一般的经验上讲: :string 被用于短文本输入(用户名, 电子邮件, 密码, 标题等).
- #:text 被用于预计文本长度较长的文本输入(描述, 评论内容等).
- # print(new_txte)
- return new_txte
- def Dingshi(num):
- '''找到好友并发送信息'''
- num =num
- try:
- bot = wxpy.Bot(cache_path=True)# 打开微信, 读取 cooker 信息 cache_path=True.
- my_friend = bot.friends().search(u'xxxxx')[0]# 从好友中找到要发送的具体的那个人
- a = get_next(num)# 在登录后调用获取获取发送信息函数
- for c in range(len(a)):# 把列表里面的额内容依次发送出去
- my_friend.send(a[c])# 发送信息
- except:pass
- # timer1=threading.Timer(1,Dingshi,(text))# 可以使用定时器进行无线发送
- # timer1.start()
- if __name__ == '__main__':
- for i in range(10):
- time.sleep(10)
- Dingshi(i)# 传这个参主要是给他里面调用 get_next 函数传值
整理代码:
- import wxpy,threading,requests,time
- from bs4 import BeautifulSoup
- def get_next(nub):
- '''获取要发送的内容'''
- url = 'http://www.59xihuan.cn/index_'
- response = requests.get(url= url + str(nub) + '.html')
- a = BeautifulSoup(response.text,'html.parser')
- txte1 = a.find_all('div',class_='pic_text1')
- new_txte = []
- for temp in txte1:
- new_txtes = temp.text
- news_text = new_txtes.replace('\r\n \r\n', '')
- news_text = news_text.replace('\r\n \n', '')# 去掉前后空格
- new_txte.append(news_text)# 获取文本内容, 注意几个获取文本的区别
- return new_txte
- def Dingshi(num):
- '''找到好友并发送信息'''
- num =num
- try:
- bot = wxpy.Bot(cache_path=True)# 打开微信, 读取 cooker 信息 cache_path=True.
- my_friend = bot.friends().search(u'xxxx')[0]# 从好友中找到要发送的具体的那个人
- a = get_next(num)# 在登录后调用获取获取发送信息函数
- for c in range(len(a)):# 把列表里面的额内容依次发送出去
- my_friend.send(a[c])# 发送信息
- except:pass
- if __name__ == '__main__':
- for i in range(10):
- time.sleep(10)
- Dingshi(i)
十秒发一次一次依次发十条, 你可以随便 sing 一发送次数只要 for i in range(10): 把这个 10 改一下就行了, 至于一次发十条是因为一页就有十条, 如果像一次发更多的话的可以让一次多读取几页存起来, 但是如果一次发太多, 会被微信监听, 然后拒接,
文章的读取可在 python 爬取小说详解 (一) 中有详讲, wxpy 会在随后进行详解一下.
来源: https://www.cnblogs.com/insane-Mr-Li/p/9118325.html