佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > python趣味挑戰(zhàn)之爬取天氣與微博熱搜并自動發(fā)給微信好友

python趣味挑戰(zhàn)之爬取天氣與微博熱搜并自動發(fā)給微信好友

熱門標(biāo)簽:美圖手機 網(wǎng)站文章發(fā)布 鐵路電話系統(tǒng) 服務(wù)器配置 呼叫中心市場需求 檢查注冊表項 智能手機 銀行業(yè)務(wù)

一、系統(tǒng)環(huán)境

1.python 3.8.2

2.webdriver(用于驅(qū)動edge)

3.微信電腦版

4.windows10

二、爬取中國天氣網(wǎng)

因為中國天氣網(wǎng)的網(wǎng)頁是動態(tài)生成的,所以不能直接爬取到數(shù)據(jù),需要先使用webdriver打開網(wǎng)頁并渲染完成,然后保存網(wǎng)頁源代碼,使用beautifulsoup分析數(shù)據(jù)。爬取的數(shù)據(jù)包括實時溫度、最高溫度與最低溫度、污染狀況、風(fēng)向和濕度、紫外線狀況、穿衣指南八項數(shù)據(jù)。

def getZZWeatherAndSendMsg():
	HTML1='http://www.weather.com.cn/weather1dn/101190201.shtml'
	driver=webdriver.Edge()
	driver.get(HTML1)
	soup=BeautifulSoup(driver.page_source,'html5lib')
	
	#獲取實時溫度
	tem=soup.find('span',class_='temp').string
	#獲取最高溫度與最低溫度
	maxtem=soup.find('span',id='maxTemp').string
	mintem=soup.find('span',id='minTemp').string
	#獲取污染狀況
	poll=soup.find('a',).string
	#獲取風(fēng)向和濕度
	win=soup.find('span',id='wind').string
	humidity=soup.find('span',id='humidity').string
	#獲取紫外線狀況
	sun=soup.find('div',class_='lv').find('em').string
	#獲取穿衣指南
	cloth=soup.find('dl',id='cy').find('dd').string

	HTML2='http://www.weather.com.cn/weathern/101190201.shtml'
	driver.get(HTML2)
	soup=BeautifulSoup(driver.page_source,'html5lib')
	#獲取天氣情況
	wea=soup.find_all('p',class_='weather-info')[1].string
	weatherContent='實時溫度:'+tem+'℃'+'\n'+'今日溫度變化:'+mintem+'~'+maxtem+'\n'+'今日天氣:'+wea+'\n'+'當(dāng)前風(fēng)向:'+win+'\n'+'相對濕度:'+humidity+'\n'+'紫外線:'+sun+'\n'+'污染指數(shù):'+poll+'\n'+'穿衣指南:'+cloth+'\n'+'注意天氣變化??!'
	driver.quit()
	return weatherContent

三、爬取微博熱搜

相比于中國天氣網(wǎng),微博熱搜要簡單很多,直接request得到數(shù)據(jù)包,然后使用beautiful解析。解析數(shù)據(jù)后用for循環(huán)便利50次保存文本。

def getWeibo():
	url='https://s.weibo.com/top/summary'
	headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.41'}
	r=requests.get(url,headers=headers)
	r.raise_for_status()
	r.encoding = r.apparent_encoding
	soup = BeautifulSoup(r.text, "html.parser")
	tr=soup.find_all('tr')
	weiboContent='今日微博熱榜:'+'\n'
	for i in range(2,52):
		text=tr[i].find('td',class_='td-02').find('a').string
		weiboContent=weiboContent+str(i-1)+'"'+text+'"'+'\n'
	return weiboContent

四、微信自動發(fā)送消息

使用win32gui自動化操作發(fā)送微信消息,首先使用微信的窗口名找到微信句柄,然后模擬鍵鼠搜索聯(lián)系人,打開聯(lián)系人窗口,發(fā)送消息并關(guān)閉窗口。同時發(fā)送多個聯(lián)系人時可以直接重復(fù)這幾步操作

if __name__=="__main__":
	target_a=['06:55','11:55','19:53']
	target_b=['07:00','12:00','19:54']
	name_list=['Squirrel B','Squirrel B']
	while True:
		now=time.strftime("%m月%d日%H:%M",time.localtime())
		print(now)
		if now[-5:] in target_a:
			base_weatherContent=getZZWeatherAndSendMsg()
			weiboContent=getWeibo()
		if now[-5:] in target_b:
			hwnd=win32gui.FindWindow("WeChatMainWndForPC", '微信')
			win32gui.ShowWindow(hwnd,win32con.SW_SHOW)
			win32gui.MoveWindow(hwnd,0,0,1000,700,True)
			time.sleep(1)
			for name in name_list:
				movePos(28,147)
				click()
				#2.移動鼠標(biāo)到搜索框,單擊,輸入要搜索的名字
				movePos(148,35)
				click()
				time.sleep(1)
				setText(name)
				ctrlV()
				time.sleep(1)  # 等待聯(lián)系人搜索成功
				enter()
				time.sleep(1)
				now=time.strftime("%m月%d日%H:%M",time.localtime())
				weatherContent='現(xiàn)在是'+now+'\n'+base_weatherContent
				setText(weatherContent)
				ctrlV()
				time.sleep(1)
				altS()
				time.sleep(1)
				setText(weiboContent)
				ctrlV()
				time.sleep(1)
				altS()
				time.sleep(1)
			win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
		time.sleep(60)

五、源代碼

import win32clipboard as w
import win32con
import win32api
import win32gui
import ctypes
import time
import requests
from urllib.request import urlopen
from bs4 import BeautifulSoup
from selenium import webdriver

#把文字放入剪貼板
def setText(aString):
	w.OpenClipboard()
	w.EmptyClipboard()
	w.SetClipboardData(win32con.CF_UNICODETEXT,aString)
	w.CloseClipboard()
	
#模擬ctrl+V
def ctrlV():
	win32api.keybd_event(17,0,0,0) #ctrl
	win32api.keybd_event(86,0,0,0) #V
	win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0)#釋放按鍵
	win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)
	
#模擬alt+s
def altS():
	win32api.keybd_event(18,0,0,0)
	win32api.keybd_event(83,0,0,0)
	win32api.keybd_event(83,0,win32con.KEYEVENTF_KEYUP,0)
	win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)
# 模擬enter
def enter():
	win32api.keybd_event(13,0,0,0)
	win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0)
#模擬單擊
def click():
	win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
	win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
#移動鼠標(biāo)的位置
def movePos(x,y):
	win32api.SetCursorPos((x,y))

def getZZWeatherAndSendMsg():
	HTML1='http://www.weather.com.cn/weather1dn/101190201.shtml'
	driver=webdriver.Edge()
	driver.get(HTML1)
	soup=BeautifulSoup(driver.page_source,'html5lib')
	
	#獲取實時溫度
	tem=soup.find('span',class_='temp').string
	#獲取最高溫度與最低溫度
	maxtem=soup.find('span',id='maxTemp').string
	mintem=soup.find('span',id='minTemp').string
	#獲取污染狀況
	poll=soup.find('a',).string
	#獲取風(fēng)向和濕度
	win=soup.find('span',id='wind').string
	humidity=soup.find('span',id='humidity').string
	#獲取紫外線狀況
	sun=soup.find('div',class_='lv').find('em').string
	#獲取穿衣指南
	cloth=soup.find('dl',id='cy').find('dd').string

	HTML2='http://www.weather.com.cn/weathern/101190201.shtml'
	driver.get(HTML2)
	soup=BeautifulSoup(driver.page_source,'html5lib')
	#獲取天氣情況
	wea=soup.find_all('p',class_='weather-info')[1].string
	weatherContent='實時溫度:'+tem+'℃'+'\n'+'今日溫度變化:'+mintem+'~'+maxtem+'\n'+'今日天氣:'+wea+'\n'+'當(dāng)前風(fēng)向:'+win+'\n'+'相對濕度:'+humidity+'\n'+'紫外線:'+sun+'\n'+'污染指數(shù):'+poll+'\n'+'穿衣指南:'+cloth+'\n'+'注意天氣變化?。?
	driver.quit()
	return weatherContent

def getWeibo():
	url='https://s.weibo.com/top/summary'
	headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.41'}
	r=requests.get(url,headers=headers)
	r.raise_for_status()
	r.encoding = r.apparent_encoding
	soup = BeautifulSoup(r.text, "html.parser")
	tr=soup.find_all('tr')
	weiboContent='今日微博熱榜:'+'\n'
	for i in range(2,52):
		text=tr[i].find('td',class_='td-02').find('a').string
		weiboContent=weiboContent+str(i-1)+'"'+text+'"'+'\n'
	return weiboContent

if __name__=="__main__":
	target_a=['06:55','11:55','19:53']
	target_b=['07:00','12:00','19:54']
	name_list=['Squirrel B','Squirrel B']
	while True:
		now=time.strftime("%m月%d日%H:%M",time.localtime())
		print(now)
		if now[-5:] in target_a:
			base_weatherContent=getZZWeatherAndSendMsg()
			weiboContent=getWeibo()
		if now[-5:] in target_b:
			hwnd=win32gui.FindWindow("WeChatMainWndForPC", '微信')
			win32gui.ShowWindow(hwnd,win32con.SW_SHOW)
			win32gui.MoveWindow(hwnd,0,0,1000,700,True)
			time.sleep(1)
			for name in name_list:
				movePos(28,147)
				click()
				#2.移動鼠標(biāo)到搜索框,單擊,輸入要搜索的名字
				movePos(148,35)
				click()
				time.sleep(1)
				setText(name)
				ctrlV()
				time.sleep(1)  # 等待聯(lián)系人搜索成功
				enter()
				time.sleep(1)
				now=time.strftime("%m月%d日%H:%M",time.localtime())
				weatherContent='現(xiàn)在是'+now+'\n'+base_weatherContent
				setText(weatherContent)
				ctrlV()
				time.sleep(1)
				altS()
				time.sleep(1)
				setText(weiboContent)
				ctrlV()
				time.sleep(1)
				altS()
				time.sleep(1)
			win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
		time.sleep(60)

六、運行效果

七、總結(jié)

  • 爬取中國天氣網(wǎng)數(shù)據(jù)
  • 爬取微博熱搜
  • 自動發(fā)送微信消息
  • 打包為exe并寫個簡單的GUI
  • 寫的比較簡單,不過也夠用了,也懶得繼續(xù)寫下去了,希望可以供大家參考.

github地址 https://github.com/gudu12306/auto_for_wechat

到此這篇關(guān)于python趣味挑戰(zhàn)之爬取天氣與微博熱搜并自動發(fā)給微信好友的文章就介紹到這了,更多相關(guān)python爬取天氣與微博熱搜內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python腳本制作天氣查詢實例代碼
  • Python實現(xiàn)天氣查詢軟件
  • python制作的天氣預(yù)報小工具(gui界面)
  • Python爬蟲之獲取心知天氣API實時天氣數(shù)據(jù)并彈窗提醒
  • Python天氣語音播報小助手

標(biāo)簽:樂山 長治 滄州 新疆 紅河 上海 河南 沈陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python趣味挑戰(zhàn)之爬取天氣與微博熱搜并自動發(fā)給微信好友》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    斗六市| 灵璧县| 拉孜县| 武功县| 英吉沙县| 改则县| 新郑市| 锦州市| 长子县| 疏勒县| 简阳市| 无为县| 南江县| 镇康县| 苍南县| 临桂县| 华亭县| 东丰县| 和平县| 威海市| 乳源| 崇义县| 德庆县| 瓮安县| 台北市| 元谋县| 大英县| 秭归县| 金华市| 四子王旗| 丰城市| 库尔勒市| 镇坪县| 阳春市| 新绛县| 富民县| 甘南县| 韶关市| 宣威市| 靖西县| 墨竹工卡县|