【python爬虫】原神公测预抽卡活动自动化抽卡脚本(一小时免登陆)
9月15号原神开始公测,公测时出了一个公测预抽卡活动,玩家可以通过这个活动,免费获取一些武器,而我总会忘记去抽,所以写个脚本偷下懒。我将写好的脚本打包成了exe,解压压缩包,运行crawl.exe,输入手机和验证码就可以自动翻卡。程序压缩包:下载:https://www.lanzoux.com/ilHukgn06te 密码:der7(亲测win10,无python环境可以运行)
活动链接:https://webstatic.mihoyo.com/ys/event/e20200829/index.html
程序运行图片
python3.7.7版本
以下是爬取的代码
import requests
import json
import time
#pyinstaller -D -i favicon.ico crawl.py
class Crawl():
#初始化cookies
def __init__(self):
self.login_ticket = "";
self.account_id="";
self.login_uid="";
self.cookie_token="";
#get请求
def get(self,url):
payload = {}
headers = {
'Cookie': 'login_uid='+self.login_uid+'; account_id='+self.account_id+'; login_ticket='+self.login_ticket+'; cookie_token='+self.cookie_token,
}
response = requests.request("GET", url, headers=headers, data = payload)
return json.loads(response.text)
#post请求
def post(self,url,data):
headers = {
'Cookie': 'login_uid='+self.login_uid+'; account_id='+self.account_id+'; login_ticket='+self.login_ticket+'; cookie_token='+self.cookie_token
}
response = requests.request("POST", url, headers=headers, data = data)
return json.loads(response.text)
def post2(self,url):
headers = {
'Cookie': 'login_uid='+self.login_uid+'; account_id='+self.account_id+'; login_ticket='+self.login_ticket+'; cookie_token='+self.cookie_token
}
response = requests.request("POST", url, headers=headers, data = {})
return json.loads(response.text)
#获取以获得的物品
def item_list(self):
url = "https://api-takumi.mihoyo.com/event/e20200828bingo/item_list"
res = self.get(url)
return res
#分享
def share(self):
url = "https://api-takumi.mihoyo.com/event/e20200828bingo/share"
res = self.get(url)
return res
#获取基本信息,是否分享,还剩翻卡次数
def home(self):
url ="https://api-takumi.mihoyo.com/event/e20200828bingo/home"
res = self.get(url)
return res
#获取验证码
def getCaptcha(self,mobile):
body ={"action_type":"login","t":"1599809011444","mobile":mobile}
url = "https://webapi.account.mihoyo.com/Api/create_mobile_captcha";
res = self.post(url,body)
return res
def login(self,mobile,captcha):
body ={"mobile_captcha":captcha,"is_bh2":"false","action_type":"login","t":"1599809011444","mobile":mobile}
url = "https://webapi.account.mihoyo.com/Api/login_by_mobilecaptcha";
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36',
}
session = requests.session()
response = session.post(url, headers=headers, data = body)
res = json.loads(response.text)
if(res["code"] == 200 and res["data"]['status'] == 1 ):
self.login_ticket = str(res["data"]['account_info']['weblogin_token']) ;
self.account_id= str(res["data"]['account_info']['account_id']);
self.login_uid= str(res["data"]['account_info']['account_id']);
cookies_url ="https://webapi.account.mihoyo.com/Api/cookie_accountinfo_by_loginticket?t=1599809011444&login_ticket="+res["data"]['account_info']['weblogin_token']
headers = {
'Cookie': 'login_uid='+self.login_uid+'; account_id='+self.account_id+'; login_ticket='+self.login_ticket
}
response = session.get(cookies_url, headers=headers)
cookies_res = json.loads(response.text)
self.cookie_token= str(cookies_res["data"]['cookie_info']['cookie_token']);
def gameinfo(self,flag):
url = "https://api-takumi.mihoyo.com/event/e20200828bingo/"+flag
if(flag == 'start'):
res = self.post(url,{})
else:
res = self.get(url)
return res['data']['game_id']
def click(self,game_id):
sum = 0
for i in range(9):
url = "https://api-takumi.mihoyo.com/event/e20200828bingo/next?game_id="+str(game_id)+"&index="+str(i);
res = self.post2(url)
if res["message"] == "OK":
sum +=1
if sum == 9:
print("本轮翻卡完毕")
def saveCookies(self):
t = time.time()
item = {
"login_ticket":self.login_ticket,
"t":t,
"account_id":self.account_id,
"login_uid":self.login_uid,
"cookie_token":self.cookie_token,
}
jsonstr = json.dumps(item,ensure_ascii=False)#转json
try:
f =open("./cookies.json",'r')
f.close()
except IOError:
f = open("./cookies.json",'w')
print("新建了文件cookies.json")
f = open("./cookies.json",'w+',encoding='utf-8')
f.write(jsonstr)
f.close()
print("cookies保存成功")
def readCookies(self):
try:
t = time.time()
f =open("./cookies.json",encoding='UTF-8')
item = json.load(f)
if t-item['t']>3600:
return False
else:
self.login_ticket = item['login_ticket']
self.account_id = item['account_id']
self.login_uid = item['login_uid']
self.cookie_token = item['cookie_token']
return True
f.close()
except IOError:
return False
if __name__ == '__main__':
#主程序
crawl = Crawl()
# 登录
if crawl.readCookies() == True:
word = input("您在一小时前登录过,是否读取cookies登录,回车确认,输入n/N进行登录")
if word != "":
print("开始登录————")
mobile = input("请输入手机号码:")
print("开始获取验证码————")
print(crawl.getCaptcha(mobile))
captcha = input("请输入验证码:")
crawl.login(mobile,captcha)
crawl.saveCookies()
else:
print("开始登录————")
mobile = input("请输入手机号码:")
print("开始获取验证码————")
print(crawl.getCaptcha(mobile))
captcha = input("请输入验证码:")
crawl.login(mobile,captcha)
crawl.saveCookies()
#分享
homeRes = crawl.home()
nowday_share_cnt = homeRes['data']['nowday_share_cnt']
if nowday_share_cnt == 0:
print("开始分享————")
print(crawl.share())
#获取基本信息
print("获取基本信息————")
homeRes = crawl.home()
lucky = homeRes['data']['lucky']
chance = homeRes['data']['chance']
print("当前幸运值:"+str(lucky))
print("可抽卡数:"+str(chance))
word = None
while word != "":
word = input("是否进行翻卡,继续请输入回车,退出请直接关闭")
if chance == 0:
print("您今天的抽卡已经到达上限,请次日再来")
word = None
while word != "":
word = input("您的今日的翻卡完毕,请输入回车关闭程序")
exit()
print("开始翻卡————")
new_round = homeRes['data']['new_round']
for index in range(chance):
print("开始第"+str(index+1)+"次翻卡")
if new_round == True:
flag = "start"
game_id = crawl.gameinfo(flag)
crawl.click(game_id)
else:
flag = "game_info"
game_id = crawl.gameinfo(flag)
crawl.click(game_id)
word = None
while word != "":
word = input("您的今日的翻卡完毕,请输入回车关闭程序")
Lwk520pj 发表于 2020-9-15 13:37
请教下楼主是用什么封装成EXE的呀?
pyinstaller -D -i favicon.ico crawl.py
如果不需要图标就删除-i favicon.ico lzawww 发表于 2020-9-15 15:14
读取cookies似乎还是用的验证码
cookies保存时间是一小时,超过就会需要登录 重复发帖了,会被删的 原神才刚开服…… 牛啊,这是个福音 厉害了,老哥,赶紧去试一下 Loker 发表于 2020-9-15 11:55
重复发帖了,会被删的
删掉之前的帖子也行,这个是更新完的 厉害,学习了 下载不了了啊 请教下楼主是用什么封装成EXE的呀? 读取cookies似乎还是用的验证码