Python使用Selenium跳过Steam年龄限制

  • 2021年7月13日
  • 技术

最近在爬取Steam的游戏信息,因为Steam的部分游戏有年龄限制,在访问游戏详情页面时会重定向到年龄选择页面,只有填写了才能访问详情。好像以前Steam设置可以修改年龄后直接访问,现在尝试不行了,所以直接使用Python的自动化测试框架Selenium。

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select

chrome_options = Options()
chrome_options.add_argument("--headless --window-size=1920,1080")  # 后台运行模式,不会弹出窗口和设置窗口大小
browser = webdriver.Chrome(r"F:\python\chromedriver_win32\chromedriver.exe" , options=chrome_options)  # 使用chrome无头浏览器

url = r"https://store.steampowered.com/app/1213740/_/"
browser.get(url)  # steam商店游戏详情(游戏有时候会进入年龄选择页面,有时候直接进入详情)
browser.implicitly_wait(2)  # 等待一定时间,让js脚本加载完毕
Select(browser.find_element_by_id("ageDay")).select_by_value("21")  # 日
Select(browser.find_element_by_id("ageMonth")).select_by_index(2)  # 月
Select(browser.find_element_by_id("ageYear")).select_by_visible_text("1995")  # 年
browser.find_element_by_class_name("btnv6_blue_hoverfade").click()  # 点击查看页面按钮

browser.get(url)  # 重新访问详情页
browser.implicitly_wait(2)
browser.save_screenshot(r"steam.png") # 网页截图
print(browser.page_source.encode('utf-8').decode())  # 打印网页源代码
browser.quit() # 结束
select方法介绍:
select_by_value:选择select对应的value值的option
select_by_index:选择select第2个option
select_by_visible_text:选择select对应内容为的option

注意:chromedriver的版本要与本机Chrome浏览器的版本一致,不然无法使用。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注