“请求购买”价格蒸汽

发布于 2025-02-13 19:32:01 字数 439 浏览 5 评论 0原文

我想从蒸汽市场获得这个价格,但是如果我尝试以这种方式获得它,

name = "P250 | Red Rock (Battle-Scarred)"
html = requests.get("https://steamcommunity.com/market/listings/730/"+name).text
soup = BeautifulSoup(html,"html5lib")

我只能获得任何价值。另一只手可以使用硒,但对我来说很慢(一个要求将近3秒)。如何获得这个数字?

Request to buy price

I want to get this price from steam market but if i try to get it this way

name = "P250 | Red Rock (Battle-Scarred)"
html = requests.get("https://steamcommunity.com/market/listings/730/"+name).text
soup = BeautifulSoup(html,"html5lib")

I only get None value. Another hand I can use Selenium but it's very slow for me (nearly 3 sec in one request). How to get this number ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2025-02-20 19:32:01

您一无所获,因为某些JavaScript在网页上动态添加了该价格元素。尝试使用BS4查找具有ID Market_Commodity_buyrequests的跨度元素的父元素(包含价格)。您会看到父级DIV元素没有元素,也没有文本。虽然在浏览器中,您会发现两个跨度元素,而休息将是文本节点。那就是问题。

使用网络工具,我看到源HTML上的JS提出了以下请求。它使用称为item_name_id标识产品的数字ID。如果您可以找到此数字ID,则可以构建URL(返回JSON响应),并使用名为buy_order_summary的键从响应中获取价格。该密钥的值具有HTML标记,并由您的原始requests.get(url)响应中缺少的内容组成。

这是定价和示例代码获取和使用的URL。

https://steamcommunity.com/market/itemordershistogram?country=US&language=english¤cy=1&item_nameid=175896332&two_factor=0
import re
import requests
from bs4 import BeautifulSoup


# finds id of the product
def get_id(s):
    id = None
    for script in s.find_all('script'):
        id_regex = re.search('Market_LoadOrderSpread\(([ 0-9]+)\)', script.text)
        if id_regex:
            id = id_regex.groups()[0].strip()
            break
    return id

name_url = "https://steamcommunity.com/market/listings/730/P250%20%7C%20Red%20Rock%20%28Battle-Scarred%29"
html = requests.get(name_url).text
soup = BeautifulSoup(html, 'lxml')
id = get_id(soup)

if id:
    id_url = f"https://steamcommunity.com/market/itemordershistogram?country=US&language=english¤cy=1&item_nameid={id}&two_factor=0"
    html = requests.get(id_url).json()
    soup = BeautifulSoup(html['buy_order_summary'], 'lxml')

    print(f"Price is {soup.select_one('span:last-child').text}")
else:
    print("Could not get ID")
    exit()

输出:

Price is $3.77

You are getting None because that price element is dynamically added on the web page by some JavaScript. Try finding parent element of the span element (which contains price) with id market_commodity_buyrequests using bs4. You'll see that parent div element has no elements and no text. While in a browser you'll find two span elements and rest would be text nodes. That's the problem.

Using network tools I saw that a JS on the source HTML makes the following request. It uses a numeric ID called item_name_id to identify the product. If you can find this numeric ID, you can construct the URL (returns JSON response) and get the price from the response using the key named buy_order_summary. This key's value has HTML markup and consists of content that is missing in your original requests.get(url) response.

Here's the URL for pricing and sample code to get and use it.

https://steamcommunity.com/market/itemordershistogram?country=US&language=english¤cy=1&item_nameid=175896332&two_factor=0
import re
import requests
from bs4 import BeautifulSoup


# finds id of the product
def get_id(s):
    id = None
    for script in s.find_all('script'):
        id_regex = re.search('Market_LoadOrderSpread\(([ 0-9]+)\)', script.text)
        if id_regex:
            id = id_regex.groups()[0].strip()
            break
    return id

name_url = "https://steamcommunity.com/market/listings/730/P250%20%7C%20Red%20Rock%20%28Battle-Scarred%29"
html = requests.get(name_url).text
soup = BeautifulSoup(html, 'lxml')
id = get_id(soup)

if id:
    id_url = f"https://steamcommunity.com/market/itemordershistogram?country=US&language=english¤cy=1&item_nameid={id}&two_factor=0"
    html = requests.get(id_url).json()
    soup = BeautifulSoup(html['buy_order_summary'], 'lxml')

    print(f"Price is {soup.select_one('span:last-child').text}")
else:
    print("Could not get ID")
    exit()

Output:

Price is $3.77
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文