首页 > 解决方案 > 在网络抓取中获得“无”

问题描述

我对 python 很陌生,但我正在尝试使用 BeautifulSoup 来收集 CSGO 皮肤的价格(特别是出售)。但是,我没有。我怀疑是因为两行 HTML 代码相同,但一个数字是请求,另一个数字是我需要的数字:销售号。有人可以帮忙吗?我特别关注新的挑战者胶囊。如果有人可以提供帮助,那将不胜感激。

import requests
from bs4 import BeautifulSoup

URLChallengerCapsule = 'https://steamcommunity.com/market/listings/730/2020%20RMR%20Challengers'
page = requests.get(URLChallengerCapsule)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(id='market_commodity_buyrequests')

ChallengerCapsulePrice = results.find('span', class_= 'market_commodity_orders_header_promote')

print(ChallengerCapsulePrice)
import requests
from bs4 import BeautifulSoup

URLChallengerCapsule = 'https://steamcommunity.com/market/listings/730/2020%20RMR%20Challengers'
page = requests.get(URLChallengerCapsule)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(id='market_commodity_buyrequests')

ChallengerCapsulePrice = results.find('span', class_= 'market_commodity_orders_header_promote')

print(ChallengerCapsulePrice)

标签: pythonweb-scraping

解决方案


BeautifulSoup 不适用于使用 JavaScript 同步填充的数据。

当您使用 cURL 加载此页面时(不支持 JavaScript),您可以看到这div是空的:

curl -s https://steamcommunity.com/market/listings/730/2020%20RMR%20Challengers | grep market_commodity_buyrequests -A 1
    <div class="market_commodity_order_summary" id="market_commodity_buyrequests">
    </div>

这里有一个很好的无头浏览器列表。你可以在 Python 中找到一些支持 Javascript 的。其他一些在线列表可能是最新的。


推荐阅读