python爬取天气与微博热搜自动发给微信好友

前言

忙着毕设与打游戏之余,突然想着写个爬虫练练手,想了想,就写了一个爬虫爬取“中国天气网”与“微博热搜”并定时发送给微信好友,放到服务器上运行了几天算是正常。
在这里插入图片描述

系统环境

  1. python 3.8.2
  2. webdriver(用于驱动edge)
  3. 微信电脑版
  4. windows10

正文

爬取中国天气网

因为中国天气网的网页是动态生成的,所以不能直接爬取到数据,需要先使用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',href='http://www.weather.com.cn/air/?city=101190201').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循环便利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

微信自动发送消息

使用win32gui自动化操作发送微信消息,首先使用微信的窗口名找到微信句柄,然后模拟键鼠搜索联系人,打开联系人窗口,发送消息并关闭窗口。同时发送多个联系人时可以直接重复这几步操作

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',href='http://www.weather.com.cn/air/?city=101190201').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)

总结

  • 爬取中国天气网数据
  • 爬取微博热搜
  • 自动发送微信消息
  • 打包为exe并写个简单的GUI
  • 写的比较简单,不过也够用了,也懒得继续写下去了,希望可以供大家参考.

github地址

https://github.com/gudu12306/auto_for_wechat

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值