【问题标题】:Why div returns empty when web-scraping Steam Game List?为什么网页抓取 Steam 游戏列表时 div 返回空?
【发布时间】:2021-03-26 12:29:49
【问题描述】:

我是网络抓取和使用 BeautifulSoup4 的新手,所以如果我的问题很明显,我很抱歉。

我正在尝试从 Steam 获取播放时间,但 <div id="games_list_rows" style="position: relative"> 返回 None 时它​​应该返回很多不同的 <div class="gameListRow" id="game_730"> 里面有东西。

我曾尝试使用朋友的个人资料,他有一些游戏,因为我认为处理大量数据可能会使 BS4 忽略 div,但它始终显示 div 为空。

这是我的代码:

import bs4 as bs
import urllib.request

# Retrieve profile
profile = "chubaquin"#input("enter profile: >")
search = "https://steamcommunity.com/id/"+profile+"/games/?tab=all"

sauce = urllib.request.urlopen(search)
soup = bs.BeautifulSoup(sauce, "lxml")

a = soup.find("div", id="games_list_rows")

print(a)

Thanks for your help!

【问题讨论】:

    标签: python html web-scraping beautifulsoup urllib


    【解决方案1】:

    网站是动态加载的,因此requests 不支持。尝试使用Selenium 作为抓取页面的替代方法。

    安装它:pip install selenium

    here下载正确的ChromeDriver。

    from time import sleep
    from bs4 import BeautifulSoup
    from selenium import webdriver
    
    
    url = "https://steamcommunity.com/id/chubaquin/games/?tab=all"
    driver = webdriver.Chrome(r"c:\path\to\chromedriver.exe")
    driver.get(url)
    # Wait for the page to fully render before parsing it
    sleep(5)
    soup = BeautifulSoup(driver.page_source, "html.parser")
    
    print(soup.find("div", id="games_list_rows"))
    

    【讨论】:

      【解决方案2】:

      你试过official Steam Web API吗? (xPaw 文档比他们自己的更好)

      您需要一个 API 密钥,但它们是免费的,而且处理 JSON 结果比抓取页面要容易得多,尤其是因为页面可能会偶尔更改,而 JSON 不太可能经常更改全部。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-07
        • 1970-01-01
        • 2021-11-15
        • 1970-01-01
        • 2021-10-01
        相关资源
        最近更新 更多