使用 PHP 和 Regex 获取 Steam 社区市场上物品的价格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23055673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:27:33  来源:igfitidea点击:

Get the price of an item on Steam Community Market with PHP and Regex

phpregexsteam

提问by Thomas Kowalski

I'm trying to use PHP to get the Steam Community Market price of an item. I take a url (for example : http://steamcommunity.com/market/listings/730/StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29) and then I download the content with file_get_contents(). I tried to use this :

我正在尝试使用 PHP 获取物品的 Steam 社区市场价格。我拿了一个网址(例如:http: //steamcommunity.com/market/listings/730/StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29)然后我下载内容file_get_contents()。我试着用这个:

function getInnerHTML($string, $tagname, $closetagname) {
    $pattern = "/<$tagname ?.*>(.*)<\/$closetagname>/";
    preg_match($pattern, $string, $matches);
    return $matches[1];
}

Using

使用

getInnerHTML($str, 'span class="market_listing_price market_listing_price_with_fee"', 'span');

An example of what I can have with file_get_contents is this :

我可以使用 file_get_contents 的一个例子是:

<span class="market_table_value">
    <span class="market_listing_price market_listing_price_with_fee">
        .92               </span>
    <span class="market_listing_price market_listing_price_without_fee">
        .68               </span>
    <br/>
</span>

But it returns nothing.

但它什么都不返回。

Has anyone an idea ?

有人有什么想法吗?

回答by Stephen Lake

Not entirely sure why you'd want to do this the hard way and regex through HTML when there's a perfectly working call which returns JSON. Although the original answer is correct and answers the OP question directly, this provides a much easier and efficient way of getting the market value of an item.

不完全确定为什么当有一个完美的工作调用返回 JSON 时,为什么要通过 HTML 以艰难的方式执行此操作并使用正则表达式。尽管原始答案是正确的并且直接回答了 OP 问题,但这提供了一种更简单有效的方法来获取商品的市场价值。

GET:

获取

http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29

http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29

JSON Response:

JSON 响应

{
  "success": true,
  "lowest_price": "1,43&#8364; ",
  "volume": "562",
  "median_price": "1,60&#8364; "
}

Response Definitions:

响应定义

success: boolean value, true if the call was successful or false if something went wrong or there are no listing for this item on the Steam market.

success: 布尔值,如果调用成功则为真,如果出现问题或在 Steam 市场上没有此物品的列表,则为假。

lowest_price: string value with currency symbol [pre-/ap]pended depending on the query parameters specified. See below for some additional parameter.

lowest_price: 带有货币符号 [pre-/ap] 的字符串值,取决于指定的查询参数。请参阅下面的一些附加参数。

volume: integer value returned as a string (?) - the total number of this specific item which has been sold/bought.

volume:作为字符串返回的整数值 (?) - 已售出/购买的此特定项目的总数。

median_price: string value with currency symbol [pre-/ap]pended. The average price at which the item has been sold. See the Steam marketplaceitem graph for a better understanding on how the median is calculated.

median_price: 带有货币符号 [pre-/ap] 的字符串值。商品已售出的平均价格。请参阅Steam 市场物品图表以更好地了解如何计算中位数。

Query Parameters:

查询参数

appid: The unique (statically defined) Steam application ID of the game/app, in our case 730 for Counter-Strike: Global Offensive. See Valve's development Wiki for a list of other appid's, though this list will most probably always be out of date as new apps are added to their platform frequently.

appid:游戏/应用程序的唯一(静态定义)Steam 应用程序 ID,在我们的示例中为 730,用于反恐精英:全球攻势。请参阅 Valve 的开发 Wiki 以获取其他 appid 的列表,尽管此列表很可能总是过时,因为新应用程序经常添加到他们的平台上。

market_hash_name: The name of the item being queried against with the exterior included, retrieving these names can be found when querying against a users inventory, but that's a whole other API call.

market_hash_name:被查询的项目的名称包括外部,在查询用户库存时可以找到检索这些名称,但这是另一个 API 调用。

currency: An integer value; the currency value and format to return the market values. You'll need to tweak and play around with these numbers as I cannot provide too much detail here. Generally I stick to using USD as a global price and use my own currency API to translate into other currencies.

currency: 一个整数值;返回市场价值的货币价值和格式。您需要调整和处理这些数字,因为我不能在这里提供太多细节。通常我坚持使用美元作为全球价格,并使用我自己的货币 API 转换为其他货币。

回答by Robin

Don't use regex for this task (see RegEx match open tags except XHTML self-contained tags, but there's a more explanatory link somewhere on SO)

不要为此任务使用正则表达式(请参阅RegEx 匹配除 XHTML 自包含标签之外的开放标签,但在 SO 某处有一个更具解释性的链接)

You want to use XPath to select your elements based on fine criteria. From PHP.netthis should get you the nodes you want:

您想使用 XPath 根据精细标准选择元素。从PHP.net这应该让你得到你想要的节点:

$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$xpath = new DOMXpath($doc);

$elements = $xpath->query('//span[@class="market_listing_price market_listing_price_with_fee"]');

the XPath //span[@class="..."]means select all spantags within the document the have the expected class attribute.

XPath//span[@class="..."]表示选择span文档中具有预期类属性的所有标签。

回答by httpNick

I created a node.js module via npm for the cs:go market. https://www.npmjs.com/package/csgo-marketIt only gets single prices at the moment, but let me know if there is additional functionality you'd like me to add.

我通过 npm 为 cs:go 市场创建了一个 node.js 模块。https://www.npmjs.com/package/csgo-market目前仅提供单一价格,但如果您希望我添加其他功能,请告诉我。