python批量查詢steam遊戲價格

最近沉迷遊戲的緊,看到HB有個30美元的包,心癢癢的很。於是寫個小工具,查下游戲價格:

一、使用方法

首先找到HB網站,將所有遊戲的名字提取下來在這裏插入圖片描述
然後新建一個Name.txt將遊戲名粘貼進去,注意每行一個
在這裏插入圖片描述
然後運行起來,如果需要輸入驗證碼就手動輸入,運行時控制檯可以看到運行日誌:
在這裏插入圖片描述
如果遊戲查找不到也會提示出來:
在這裏插入圖片描述
最後會自動生成詳細數據的表格文件Data.csv:
在這裏插入圖片描述
這樣一下子能看到史低價格和好評率,就能決定買不買啦!

二、代碼

"""
@author: Bre Athy
@contact: https://www.zhihu.com/people/you-yi-shi-de-hu-xi
@productware: PyCharm
@file: steam價格批量查詢.py
@time: 2020/4/2 0:32
"""
import time

from selenium.webdriver import Chrome,ChromeOptions
import selenium.webdriver.support.ui as ui
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import re


url = "https://steamdb.info/"
headUrl = "https://steamdb.info/search/?a=app&q="
tailUrl = "&type=1&category=0"
appUrl = "https://steamdb.info/app/"


def searchGameAppidDB(appid):
    driver.get(appUrl+appid)
    try:
        table = ui.WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='table-responsive']")))
    except TimeoutException:
        table = None
    if table:
        tds = table.find_elements_by_xpath("./table/tbody/tr[@class='table-prices-current']/td")
        c_price = re.search(r"\d+", tds[1].text).group()
        b_price = re.search(r"\d+", tds[3].text).group()
        print("當前價格:",c_price)
        print("史低價格:",b_price)
        p_rate_old = driver.find_elements_by_xpath("//div[contains(@class, 'header-thing-number')]")[0].text.strip()
        p_rate = re.search(r".* (.*)", p_rate_old).group(1)
        in_game_old = driver.find_elements_by_xpath("//div[contains(@class, 'header-thing-number')]")[1].text.strip()
        in_game = "".join(in_game_old.split(","))
        print("好評率:",p_rate)
        print("正在遊玩:",in_game)
        r_date = driver.find_element_by_xpath("//table[contains(@class,'table-responsive-flex')]//tr[last()]/td[last()]").text.strip()
        print("發行日期:",r_date)
        return c_price+","+b_price+","+p_rate+","+in_game+","+r_date
    else:return False



def searchGameNameDB(game):
    driver.get(headUrl+game+tailUrl)
    appid = None
    try:
        table = ui.WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//table[@id='table-sortable']")))
    except TimeoutException:
        table = None
    if table:
        trs = table.find_elements_by_xpath("./tbody/tr")
        if len(trs) == 1:
            appid = trs[0].find_elements_by_xpath("./td")[0].text
            versions = ""
        else:
            print("該遊戲存在多個版本:")
            appids = []
            versions = ""
            for tr in trs:
                name = tr.find_elements_by_xpath("./td")[2].text
                versions += name+","
                print(name)
                if "".join(game.split()).lower() == "".join(name.split()).lower():
                    appids.append(tr.find_elements_by_xpath("./td")[0].text)
            if appids[0]:appid = appids[0]
        if appid:
            return searchGameAppidDB(appid)+","+versions
        else:
            print("該遊戲未找到相關信息")
            return False
    else:
        print("該遊戲未找到相關信息")
        return False


if __name__ == "__main__":
    # 讀取遊戲名
    games = []
    file = open("Name.txt", encoding="utf-8")
    for line in file:
        games.append(line.replace("\n",""))
    file.close()
    print("本次共查詢"+str(len(games))+"條項目")
    # 增加瀏覽器擴展防止爬蟲檢測
    option = ChromeOptions()
    option.add_experimental_option('excludeSwitches', ['enable-automation'])
    driver = Chrome(options=option)
    # 開始查詢
    driver.get(url)
    print(8*"*"+" 開始查詢 "+8*"*")

    i = 1
    result = "序號,遊戲名,當前價格,史低價格,好評率,正在遊玩,發行日期,其他版本\n"
    for game in games:
        print("-"*5,"第%d條"%i,"-"*5)
        print(game)
        str_search = searchGameNameDB(game)
        if str_search:
            result += str(i)+","+game+","+str_search+"\n"
        else:
            result += str(i)+","+game+","+"未查找到相關遊戲信息"+","+headUrl+game+tailUrl+"\n"
        i += 1

    driver.close()

    with open("data.csv","w",encoding="utf-8-sig")as f:
        f.write(result)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章