首页 > 解决方案 > Python Beautiful Soup 和 urllib.request - 如何通过 Steam 年龄检查

问题描述

我正在尝试制作一个从为期一周的交易中获取信息的 Steam 解析器。

但是,有些项目会被年龄检查阻止。我正在使用 urllib.request 和 Beautiful Soup 4 来获取信息,但正如您可能已经猜到的那样,我无法获得 M 级项目。我尝试搜索类似的问题,但没有人告诉我如何使用 urllib.request 通过年龄检查

仅当项目实际上没有描述时,我才希望测试等于“无描述”

这是我的代码:

import urllib.request

import bs4 as bs

source = urllib.request.urlopen('https://store.steampowered.com/search/?filter=weeklongdeals')
soup = bs.BeautifulSoup(source,'lxml')

searchResultContainer = soup.find('div',{'id':'search_result_container'})
containerHolder = searchResultContainer.findChildren()[1]

links = []
for a in containerHolder.findAll('a', href=True):
    links.append(a['href'])

x = 0
description = []
for link in links:
    source = urllib.request.urlopen(str(link))
    soup = bs.BeautifulSoup(source,'lxml')

    try: 
        test = soup.find('div',{'class':'game_description_snippet'}).get_text().strip()
        description.append(soup.find('div',{'class':'game_description_snippet'}).get_text().strip())
    except:
        test = 'No description'
        description.append('No description')
    finally:
        x += 1
        print(f'{x}: {test}')

标签: pythonparsingbeautifulsoupsteamurllib3

解决方案


我确定年龄选择保存在 cookie 中,因此您需要保存该 cookie 并将其用于您的会话。

我通常会建议使用请求以方便使用,应该让它快速而轻松。


推荐阅读