Get the price of an item on Steam Community Market with PHP and Regex
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.
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
JSON Response:
{
"success": true,
"lowest_price": "1,43€ ",
"volume": "562",
"median_price": "1,60€ "
}
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.
lowest_price
: string value with currency symbol [pre-/ap]pended depending on the query parameters specified. See below for some additional parameter.
volume
: integer value returned as a string (?) - the total number of this specific item which has been sold/bought.
median_price
: string value with currency symbol [pre-/ap]pended. The average price at which the item has been sold. See the Steam marketplace item graph for a better understanding on how the median is calculated.
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.
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.
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.
This is an undocumented endpoint and therefore might not be permanent, or may be subject to change, nobody knows.
Get price for item on steam community market
Thats how you get the prices for the first page,work a way to identify the currency/loop threw the pages/sort them and you are done.
<?php
$string = file_get_contents('http://steamcommunity.com/market/listings/730/AK-47%20|%20Vulcan%20%28Battle-Scarred%29');
$attrList = explode('<span class="market_listing_price market_listing_price_with_fee">',$string);
$N=count($attrList);
for ($i=1;$i<$N;$i++){
$prices[$i-1] = explode('</span>',$attrList[$i])[0];
}
print_r($prices);
?>
Steam Market API?
I could not find any documentation, but I use:
http://steamcommunity.com/market/priceoverview/?appid=730¤cy=3&market_hash_name=StatTrak%E2%84%A2 M4A1-S | Hyper Beast (Minimal Wear)
to return a JSON.
At time of writing, it returns:
{"success":true,"lowest_price":"261,35€ ","volume":"11","median_price":"269,52€ "}
You can change the currency. 1 is USD, 3 is euro but there are probably others.
Get steam item prices
I use
render?start=0&count=10¤cy=3&language=english&format=json
currency: 1 for USD, 2 for GBP, 3 for EUR, 5 for RUB
http://steamcommunity.com/market/listings/730/StatTrak%E2%84%A2%20AK-47%20%7C%20Blue%20Laminate%20%28Factory%20New%29/render?start=0&count=10¤cy=3&language=english&format=json
Request to buy price Steam
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
Related Topics
Codeigniter Assets Folder Best Practice
Php's File_Exists() Will Not Work for Me
Connection Reset by Peer: Mod_Fcgid: Error Reading Data from Fastcgi Server
How to Detect Non-Ascii Characters in a String
PHP Pdo with Foreach and Fetch
Magento - Redirect Customer from Observer Method
Upper Memory Limit for PHP/Apache
Can Anyone Get Access to My PHP Source Code
How to Use PHP in a JavaScript Function
Check If Laravel Model Got Saved or Query Got Executed
Openssl Not Working on Windows, Errors 0X02001003 0X2006D080 0X0E064002
Convert to Date Format Dd/Mm/Yyyy
Add a Quantity Field to Ajax Add to Cart Button on Woocommerce Shop Page
How to Convert an Image to Black and White in PHP