欢迎光临
我们一直在努力

利用Python白玩steam游戏 我是专业的

做自己喜欢的爬虫:steam白玩

闲的无聊做了一期steam白嫖的文章。
有了它,以后就再也不会担心游戏没有白玩到了。
特地做出来个exe可执行文件给大家用,免得电脑没有python的人眼馋。


私信小编01即可获取大量Python学习资料

话不多说,直接上Python代码:

import csv
import sys
import requests
from lxml import etree
from threading import Thread
from tkinter import messagebox
import pandas as pd
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"}

def login(url="https://steamdb.info/upcoming/free/"):
    try:
        html = requests.get(url,headers = headers)
        if html.status_code == 200:
            text = html.text
            dom = etree.HTML(text)
            return dom
        else:
            pass
    except:
        messagebox.showinfo("warning","warn Internet")
        sys.exit(0)
def now(dom):
    name = dom.xpath('//table[1]//a/b/text()')
    price = dom.xpath('//table[1]//td[4]/text()')
    start_time = dom.xpath('//table[1]//td[5]/@title')
    end_time = dom.xpath('//table[1]//td[6]/@title')
    for a,b,c,d in zip(name,price,start_time,end_time):
        name,price,start,end = replace(a,b,c,d)
        write_csv(name,price,start,end,"now.csv")
def furture(dom):
    name = dom.xpath('//table[2]//a/b/text()')
    price = dom.xpath('//table[2]//td[3]/text()')
    start_time = dom.xpath('//table[2]//td[4]/@title')
    end_time = dom.xpath('//table[2]//td[5]/@title')
    for a,b,c,d in zip(name,price,start_time,end_time):
        name,price,start,end = replace(a,b,c,d)
        write_csv(name,price,start,end,"furture.csv")
def replace(name,price,start,end):
    start = start.split("+")[0]
    end = end.split("+")[0]
    if price != "Weekend":
        name = name.split("Limited")[0]
        price = "Free"
    else:
        name = name.split("Free")[0]
    return name,price,start,end
def write_csv(name,price,start,end,csv_name):
    with open(csv_name,"a",newline="") as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow([name,price,start,end])
    print(name,price,start,end)
def read_csv(csv_name):
    url = "https://store.steampowered.com/search/?term="
    csv_data = pd.read_csv(csv_name,header = None,encoding = "utf-8")
    names = csv_data[0]
    start_times = csv_data[2]
    end_times = csv_data[3]
    count = 1
    for name,start_time,end_time in zip(names,start_times,end_times):
        name = name[:-1]
        new_name = "+".join(name.split())
        dom = login(url+new_name)
        store_name = dom.xpath('//*[@id="search_resultsRows"]/a[1]/div[2]/div[1]/span/text()')[0]
        if name == store_name:
            count += 1
            store = dom.xpath('//*[@id="search_resultsRows"]/a[1]/@href')[0]
            start_time = "日".join("月".join(start_time.split("-")[1:]).split("T"))
            end_time = "日".join("月".join(end_time.split("-")[1:]).split("T"))
            write_txt("【"+name+"】	领取时间为:{}-{}
".format(start_time,end_time))
            write_txt("领取地址为:{}
".format(store))
            write_txt("{}
".format("-"*80))
            print("写入{}次".format(count-1))
        else:
            count += 1
            write_txt("第{}项出错,请去csv内部检测
".format(count-1))
            write_txt("游戏名字为:{}".format(name))
            write_txt("{}
".format("-"*80))
def write_txt(text):
    with open("steam.txt","a") as f:
        f.write(text)
def run_write(csv_name):
    read_csv(csv_name)
    write_txt("-"*40,"这里是分割线","-"*40)
if __name__ == "__main__":
    threads = []
    dom = login()
    threads.append(Thread(target=now,args=(dom,)))
    threads.append(Thread(target=furture,args=(dom,)))
    for thread in threads:
        thread.start()
        thread.join()
    run_write("now.csv")
    run_write("furture.csv")

 

 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

未经允许不得转载:英协网 » 利用Python白玩steam游戏 我是专业的

分享到: 生成海报
avatar

热门文章

  • 评论 抢沙发

    • QQ号
    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址

    登录

    忘记密码 ?

    切换登录

    注册

    我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活