在网页抓取中获得“无”

时间:2021-01-30 01:09:57

标签: python web-scraping

我对 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)

1 个答案:

答案 0 :(得分:0)

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>

有一个很好的 headless browsers here 列表。您可以在 Python 中找到其中一些确实支持 Javascript。其他一些在线列表可能是最新的。