网络知识 娱乐 python获取steam/epic喜加一信息并自动发送到微信

python获取steam/epic喜加一信息并自动发送到微信

前言

免费的就是最好玩的,对于steam/epic上免费送的游戏我们当然一个也不想错过。尤其是epic上的每周免费,动不动就是大作。但是每天去网站搜索喜加一信息也不是办法,因此写了一个程序,代码比较简单

python获取steam/epic喜加一信息并自动发送到微信

爬虫

  • 网站为https://steamstats.cn/xi
  • 通过requests.get()方法得到网页内容,使用beautifulsoup解析内容并提取数据
  • 通过标签定位我们需要的内容,主要是一个tbody,逐行读取并存储数据
  • 将存储的变量存入text变量并返回

自动发送微信

使用win32clipboard库模拟ctrl+v与als+s与enter、剪贴板、鼠标单击等 将这些动作模拟出来之后,即可以实现微信自动发送内容

python获取steam/epic喜加一信息并自动发送到微信

python获取steam/epic喜加一信息并自动发送到微信

源代码

import win32clipboard as wnimport win32connimport win32apinimport win32guinimport ctypesnimport timenimport requestsnfrom urllib.request import urlopennfrom bs4 import BeautifulSoupnn#把文字放入剪贴板ndef setText(aString):ntw.OpenClipboard()ntw.EmptyClipboard()ntw.SetClipboardData(win32con.CF_UNICODETEXT,aString)ntw.CloseClipboard()ntn#模拟ctrl+Vndef ctrlV():ntwin32api.keybd_event(17,0,0,0) #ctrlntwin32api.keybd_event(86,0,0,0) #Vntwin32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0)#释放按键ntwin32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)ntn#模拟alt+sndef altS():ntwin32api.keybd_event(18,0,0,0)ntwin32api.keybd_event(83,0,0,0)ntwin32api.keybd_event(83,0,win32con.KEYEVENTF_KEYUP,0)ntwin32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)n# 模拟enterndef enter():ntwin32api.keybd_event(13,0,0,0)ntwin32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0)n#模拟单击ndef click():ntwin32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)ntwin32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)n#移动鼠标的位置ndef movePos(x,y):ntwin32api.SetCursorPos((x,y))nndef get_free():nturl='https://steamstats.cn/xi'ntheaders={'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'}ntr=requests.get(url,headers=headers)ntr.raise_for_status()ntr.encoding = r.apparent_encodingntsoup = BeautifulSoup(r.text, "html.parser")nttbody=soup.find('tbody')nttr=tbody.find_all('tr')nti=1nttext="今日喜加一"+'n'ntfor tr in tr:ntttd=tr.find_all('td')nttname=td[1].string.strip().replace('n', '').replace('r', '')nttgametype=td[2].string.replace(" ","").replace('n', '').replace('r', '')nttstart=td[3].string.replace(" ","").replace('n', '').replace('r', '')nttend=td[4].string.replace(" ","").replace('n', '').replace('r', '')ntttime=td[5].string.replace(" ","").replace('n', '').replace('r', '')nttoringin=td[6].find('span').string.replace(" ","").replace('n', '').replace('r', '')ntttext=text+"序号:"+str(i)+'n'+"游戏名称:"+name+'n'+"DLC/game:"+gametype+'n'+"开始时间:"+start+'n'+"结束时间:"+end+'n'+"是否永久:"+time+'n'+"平台:"+oringin+'n'ntreturn textnnif __name__=="__main__":nttarget_a=['7:55']nttarget_b=['8:00']ntname_list=['Squirrel C']ntwhile True:nttnow=time.strftime("%m月%d日%H:%M",time.localtime())nttprint(now)nttif now[-5:] in target_a:nttttext=get_free()nttif now[-5:] in target_b:nttthwnd=win32gui.FindWindow("WeChatMainWndForPC", '微信')ntttwin32gui.ShowWindow(hwnd,win32con.SW_SHOW)ntttwin32gui.MoveWindow(hwnd,0,0,1000,700,True)nttttime.sleep(1)ntttfor name in name_list:nttttmovePos(28,147)nttttclick()ntttt#2.移动鼠标到搜索框,单击,输入要搜索的名字nttttmovePos(148,35)nttttclick()ntttttime.sleep(1)nttttsetText(name)nttttctrlV()ntttttime.sleep(1) # 等待联系人搜索成功nttttenter()ntttttime.sleep(1)nttttnow=time.strftime("%m月%d日%H:%M",time.localtime())ntttttext='现在是'+now+'n'+textnttttsetText(text)nttttctrlV()ntttttime.sleep(1)nttttaltS()ntttttime.sleep(1)ntttwin32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)ntttime.sleep(60)

外链

推荐将代码放到服务器或者树莓派就不用整天开着电脑了