python爬取steam/epic喜加一信息高效白嫖

本文介绍了如何使用Python爬虫从steamstats.cn获取免费游戏信息,并结合win32clipboard库自动发送微信消息。通过设置定时检查时间,当有新喜加一时,程序会模拟键盘和鼠标操作在微信中搜索并发送游戏详情。
摘要由CSDN通过智能技术生成

python爬虫之爬取steam/epic喜加一信息

前言

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

Created with Raphaël 2.3.0 开始 设定时间? 爬取喜加一数据 发送微信消息 休眠一分钟 退出? 结束 yes no yes no

爬虫

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

自动发送微信

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

鼠标单击搜索框
复制联系人ctrl+v
回车enter
复制内容ctrl+v
发送alt+s
将联系人放入剪贴板
将发送内容放入剪贴板

在这里插入图片描述

源代码

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

#把文字放入剪贴板
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 get_free():
	url='https://steamstats.cn/xi'
	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")
	tbody=soup.find('tbody')
	tr=tbody.find_all('tr')
	i=1
	text="今日喜加一"+'\n'
	for tr in tr:
		td=tr.find_all('td')
		name=td[1].string.strip().replace('\n', '').replace('\r', '')
		gametype=td[2].string.replace(" ","").replace('\n', '').replace('\r', '')
		start=td[3].string.replace(" ","").replace('\n', '').replace('\r', '')
		end=td[4].string.replace(" ","").replace('\n', '').replace('\r', '')
		time=td[5].string.replace(" ","").replace('\n', '').replace('\r', '')
		oringin=td[6].find('span').string.replace(" ","").replace('\n', '').replace('\r', '')
		text=text+"序号:"+str(i)+'\n'+"游戏名称:"+name+'\n'+"DLC/game:"+gametype+'\n'+"开始时间:"+start+'\n'+"结束时间:"+end+'\n'+"是否永久:"+time+'\n'+"平台:"+oringin+'\n'
	return text

if __name__=="__main__":
	target_a=['7:55']
	target_b=['8:00']
	name_list=['Squirrel C']
	while True:
		now=time.strftime("%m月%d日%H:%M",time.localtime())
		print(now)
		if now[-5:] in target_a:
			text=get_free()
		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())
				text='现在是'+now+'\n'+text
				setText(text)
				ctrlV()
				time.sleep(1)
				altS()
				time.sleep(1)
			win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
		time.sleep(60)

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

外链

github源代码https://github.com/gudu12306/steam-epic
知乎博客https://zhuanlan.zhihu.com/p/421129986

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值