利用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會在隨后進行詳解一下。
