Python通知Epic白嫖游戏信息

每周都有免费游戏 - Epic Games
近期看到Epic在送游戏,目前每周都会有活动白嫖。

身为白嫖党,肯定要操作一下。

游戏列表:Epic Games Store 每周免费游戏(331) | indienova GameDB 游戏库

大致思路:

1、根据网站,获取可 “  白嫖  ”的游戏

2、处理相关信息,组成文本

3、发送到微信上,让我们知道。

1、查询网站

下面网页,会发布最新免费,可白嫖的游戏。我们爬取这些信息,进行判断

游戏列表:Epic Games Store 每周免费游戏(331) | indienova GameDB 游戏库

Python通知Epic白嫖游戏信息_第1张图片

2、代码编写

1.我们爬取网页的数据

2.获取网页中所有游戏的信息

3.判断游戏信息是最新编辑的

4.汇总信息进行发送到微信

相关实例代码

# -*- coding: utf-8 -*-
# @Time    : 2022/12/29 16:33
# @Author  : 南宫乘风
# @Email   : [email protected]
# @File    : epic_all.py
# @Software: PyCharm
import json
import re
import time

import requests
from bs4 import BeautifulSoup


def get_url_info():
    url = 'https://indienova.com/gamedb/list/121/p/1'
    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'}
    res = requests.get(url, headers=headers).text
    return res


def check_epci_info(game_info_list):
    list_content = []
    for i in game_info_list:
        game_time = i['game_time']
        if '小时前' in game_time[0]:
            content = f"中文名称:{i['game_zh']}  
" \ f"英文名称:{i['game_en']}
" \ f"领取时间:{i['game_start']}
" \ f"发布时间:{i['game_time']}

" list_content.append(content) return list_content def parse_web_page(res): soup = BeautifulSoup(res, "html.parser") res.encode('UTF-8').decode('UTF-8') div_class = soup.find(name='div', attrs={"id": "portfolioList"}) # print(div_class[0]) game_name = div_class.find_all(name='div', attrs={"class": "col-xs-12 col-sm-6 col-md-4 user-game-list-item"}) list_game = str(game_name).split('
') game_info_list = [] for i in list_game[1:]: dict_info = {} # print('----------------------------------------------------------------------------------') game = BeautifulSoup(i, "html.parser") game_all_info = game.find(name='h4') game_name_zh = game_all_info.find_all(name='a') game_name_en = game_all_info.find_all(name='small') game_name_zh = re.findall(r'>(.+?)<', str(game_name_zh)) game_name_en = re.findall(r'>(.+?)<', str(game_name_en)) # print(game_name_zh, game_name_en) game_start_end = game.find(name='p', attrs={"class": "intro"}) game_start_end_new = game_start_end.find_all(name='span') game_edit_time = game.find(name='p', attrs={"class": "text-date"}) game_edit_time_new = game_edit_time.find_all(name='small') game_edit_time_new = str(game_edit_time_new).replace(" ", "").replace("\n", " ") game_start_end_new = re.findall(r'>(.+?)<', str(game_start_end_new)) game_edit_time_new = re.findall(r'>(.+?)<', str(game_edit_time_new)) dict_info["game_zh"] = game_name_zh dict_info["game_en"] = game_name_en dict_info["game_start"] = game_start_end_new dict_info["game_time"] = game_edit_time_new game_info_list.append(dict_info) # print(game_start_end_new,game_edit_time_new) return game_info_list def send_to_epic_message(list_content): content = ''.join(list_content) + '\nhttps://indienova.com/gamedb/list/121/p/1' token = 'tokenxxxxxxxxxxxxxxxxxxxx' day_time = time.strftime('%Y-%m-%d', time.localtime(time.time())) title = f'Epic免费游戏-{day_time}' # 改成你要的标题内容 error_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) # 获取格式化的时间 url = 'http://www.pushplus.plus/send' data = { "token": token, # 密钥 "title": title, # 标题 "content": content, # 发送的信息内容,这里我们是json数组 "template": "markdown" # 数据类型为json } body = json.dumps(data).encode(encoding='utf-8') headers = {'Content-Type': 'application/json'} request_result = requests.post(url, data=body, headers=headers) # print(request_result) # if __name__ == '__main__': res = get_url_info() game_info_list = parse_web_page(res) list_content = check_epci_info(game_info_list) send_to_epic_message(list_content)

3、发送平台(pushplus)

微信公众号关注

pushplus(推送加)-微信消息推送平台

获取token进行配置即可。

4、定时任务

我们要把脚本部署到Linux操作环境 上。

首先记得安装依赖。pip install 模块

定时任务,每天10点运行一次。

0 10 * * * /usr/bin/python3 /opt/epic_send.py

Python通知Epic白嫖游戏信息_第2张图片

Python通知Epic白嫖游戏信息_第3张图片

5、增加 喜加一 的资讯通知

网站为steam free game,steam free promotion - Steam Stats

Python通知Epic白嫖游戏信息_第4张图片

# -*- coding: utf-8 -*-
# @Time    : 2022/12/29 15:51
# @Author  : 南宫乘风
# @Email   : [email protected]
# @File    : epic.py
# @Software: PyCharm
import json
import time

import requests
from bs4 import BeautifulSoup


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")
    text = "今日喜加一 
" + 'https://steamstats.cn/xi' + '
' tbody = soup.find('tbody') tr = tbody.find_all('tr') i = 1 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) + '
' + "游戏名称:" + name + '
' + "DLC/game:" + gametype + '
' + "开始时间:" + start + '
' + "结束时间:" + end + '
' + "是否永久:" + time + '
' + "平台:" + oringin + '
' # print(text) i++ return text def send_to_epic_message(text_info): content = ''.join(text_info) token = 'xxxxxxxxxxxxxxxxx' day_time = time.strftime('%Y-%m-%d', time.localtime(time.time())) title = f'喜加一 免费游戏-{day_time}' # 改成你要的标题内容 error_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) # 获取格式化的时间 url = 'http://www.pushplus.plus/send' data = { "token": token, # 密钥 "title": title, # 标题 "content": content, # 发送的信息内容,这里我们是json数组 "template": "markdown" # 数据类型为json } body = json.dumps(data).encode(encoding='utf-8') headers = {'Content-Type': 'application/json'} request_result = requests.post(url, data=body, headers=headers) if __name__ == "__main__": game_info = get_free() if len(game_info) > 40: send_to_epic_message(get_free())

Python通知Epic白嫖游戏信息_第5张图片

 

 

参考文档:python获取steam/epic喜加一信息并自动发送到微信 - 知乎

你可能感兴趣的:(Python学习,游戏)