最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年05月31日 15:24:05   作者:gudu12306  
忙著畢設與打游戲之余,突然想著寫個爬蟲練練手,想了想,就寫了一個爬蟲爬取“中國天氣網”與“微博熱搜”并定時發(fā)送給微信好友,放到服務器上運行了幾天算是正常,需要的朋友可以參考下

一、系統環(huán)境

1.python 3.8.2

2.webdriver(用于驅動edge)

3.微信電腦版

4.windows10

二、爬取中國天氣網

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

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
	#獲取風向和濕度
	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'+'當前風向:'+win+'\n'+'相對濕度:'+humidity+'\n'+'紫外線:'+sun+'\n'+'污染指數:'+poll+'\n'+'穿衣指南:'+cloth+'\n'+'注意天氣變化!!'
	driver.quit()
	return weatherContent

三、爬取微博熱搜

相比于中國天氣網,微博熱搜要簡單很多,直接request得到數據包,然后使用beautiful解析。解析數據后用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ā)送微信消息,首先使用微信的窗口名找到微信句柄,然后模擬鍵鼠搜索聯系人,打開聯系人窗口,發(fā)送消息并關閉窗口。同時發(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.移動鼠標到搜索框,單擊,輸入要搜索的名字
				movePos(148,35)
				click()
				time.sleep(1)
				setText(name)
				ctrlV()
				time.sleep(1)  # 等待聯系人搜索成功
				enter()
				time.sleep(1)
				now=time.strftime("%m月%d日%H:%M",time.localtime())
				weatherContent='現在是'+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)
#移動鼠標的位置
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
	#獲取風向和濕度
	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'+'當前風向:'+win+'\n'+'相對濕度:'+humidity+'\n'+'紫外線:'+sun+'\n'+'污染指數:'+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.移動鼠標到搜索框,單擊,輸入要搜索的名字
				movePos(148,35)
				click()
				time.sleep(1)
				setText(name)
				ctrlV()
				time.sleep(1)  # 等待聯系人搜索成功
				enter()
				time.sleep(1)
				now=time.strftime("%m月%d日%H:%M",time.localtime())
				weatherContent='現在是'+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)

六、運行效果

在這里插入圖片描述

七、總結

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

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

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

相關文章

  • 使用Python防止SQL注入攻擊的實現示例

    使用Python防止SQL注入攻擊的實現示例

    這篇文章主要介紹了使用Python防止SQL注入攻擊的實現示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • python中re.findall函數實例用法

    python中re.findall函數實例用法

    在本篇文章里小編給大家整理了一篇關于python中re.findall函數實例用法相關內容,有興趣的朋友們可以學習下。
    2021-09-09
  • 使用Python來開發(fā)Markdown腳本擴展的實例分享

    使用Python來開發(fā)Markdown腳本擴展的實例分享

    這篇文章主要介紹了使用Python來開發(fā)Markdown腳本擴展的實例分享,文中的示例是用來簡單地轉換文檔結構,主要為了體現一個思路,需要的朋友可以參考下
    2016-03-03
  • pytorch中nn.Conv1d的用法詳解

    pytorch中nn.Conv1d的用法詳解

    今天小編就為大家分享一篇pytorch中nn.Conv1d的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python使用smtplib庫發(fā)送電子郵件

    Python使用smtplib庫發(fā)送電子郵件

    Python提供了smtplib庫,用于發(fā)送電子郵件,本文將詳細介紹如何使用Python的smtplib庫來發(fā)送電子郵件,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-11-11
  • python之tensorflow手把手實例講解貓狗識別實現

    python之tensorflow手把手實例講解貓狗識別實現

    要說到深度學習圖像分類的經典案例之一,那就是貓狗大戰(zhàn)了。貓和狗在外觀上的差別還是挺明顯的,無論是體型、四肢、臉龐和毛發(fā)等等, 都是能通過肉眼很容易區(qū)分的。那么如何讓機器來識別貓和狗呢?網上已經有不少人寫過這案例了,我也來嘗試下練練手。
    2021-09-09
  • 小白如何入門Python? 制作一個網站為例

    小白如何入門Python? 制作一個網站為例

    以制作一個網站為例,聊一聊小白如何入門Python,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • 更改Python的pip install 默認安裝依賴路徑方法詳解

    更改Python的pip install 默認安裝依賴路徑方法詳解

    今天小編就為大家分享一篇更改Python的pip install 默認安裝依賴路徑方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python科學計算環(huán)境推薦——Anaconda

    Python科學計算環(huán)境推薦——Anaconda

    最近在用Python做中文自然語言處理。使用的IDE是PyCharm。PyCharm確實是Python開發(fā)之首選,但用于科學計算方面,還略有欠缺。為此我嘗試過Enthought Canopy,但Canopy感覺把問題搞得復雜化,管理Python擴展也不太方便。直到今天我發(fā)現了 Anaconda 。
    2014-06-06
  • 在Python中marshal對象序列化的相關知識

    在Python中marshal對象序列化的相關知識

    這篇文章主要介紹了在Python中marshal對象序列化的相關知識,是Python進階學習中序列化相關的知識,需要的朋友可以參考下
    2015-07-07

最新評論

平凉市| 桃源县| 邯郸县| 城固县| 藁城市| 朝阳市| 长白| 邳州市| 康定县| 晋宁县| 石渠县| 金溪县| 阿拉善盟| 原平市| 通山县| 浦县| 宿迁市| 余姚市| 潞城市| 梧州市| 庆城县| 民县| 屏东县| 西峡县| 汝州市| 金塔县| 辽阳县| 辽源市| 连云港市| 安顺市| 屏边| 神农架林区| 那坡县| 两当县| 百色市| 武陟县| 定陶县| 隆德县| 黄龙县| 兴业县| 莆田市|