Comprehensive Guide to Fetching and Recording Counter-Strike Item Prices
Automate the Retrieval of Lowest Prices for CS Items and Export Them to Excel
Key Takeaways
- Efficient Data Retrieval: Leveraging Python's robust libraries allows seamless interaction with the Steam Market API to fetch real-time prices.
- Structured Data Export: Automating the process ensures that the lowest prices for each item are accurately recorded in an organized Excel spreadsheet.
- Error Handling and Rate Limiting: Implementing proper error handling and adhering to rate limits enhances the script's reliability and prevents potential blocking by the API provider.
Introduction
In the dynamic world of Counter-Strike, item prices fluctuate frequently based on supply, demand, and in-game events. For players and traders aiming to stay informed about the latest prices, automating the retrieval and recording of these values can be immensely beneficial. This guide provides a comprehensive Python script that iterates through a predefined list of Counter-Strike items, fetches their lowest prices from the Steam Community Market API, and records the data vertically into an Excel spreadsheet.
Prerequisites
Before diving into the script, ensure that your system meets the following requirements:
- Python Installation: Make sure Python is installed on your system. You can download it from the official Python website.
- Required Libraries: The script utilizes several Python libraries that need to be installed. Open your terminal or command prompt and execute the following command:
pip install requests openpyxl pandas - Internet Connection: A stable internet connection is necessary to interact with the Steam Community Market API.
Script Overview
The Python script is structured to perform the following tasks sequentially:
- Define the Item List: A predefined list of Counter-Strike items is established, which the script will iterate through.
- API Interaction: For each item, the script constructs the appropriate API URL, sends a request, and retrieves the lowest price from the JSON response.
- Data Recording: The fetched prices are systematically written into an Excel spreadsheet, ensuring each item's price is recorded in a vertical format.
- Error Handling and Rate Limiting: The script incorporates mechanisms to handle potential errors and adheres to rate limiting by introducing delays between API requests.
Detailed Explanation
1. Importing Necessary Libraries
The script begins by importing essential Python libraries:
import requests
import time
import pandas as pd
from urllib.parse import quote
- requests: Facilitates HTTP requests to interact with the Steam Market API.
- time: Allows the script to introduce delays, preventing rapid consecutive requests that might trigger rate limiting.
- pandas: A powerful data manipulation library used here to organize and export data to Excel.
- urllib.parse.quote: Assists in URL encoding item names to ensure they are correctly formatted for API requests.
2. Defining the Item List
The item list is a crucial component, containing all the Counter-Strike items for which prices need to be fetched:
items = [
"Sealed Graffiti | 8-Ball (Bazooka Pink)",
"Sealed Graffiti | 8-Ball (Cash Green)",
"Sealed Graffiti | Bling (Blood Red)",
"Sealed Graffiti | Bling (Monarch Blue)",
"Sealed Graffiti | Bling (Monster Purple)",
"Sealed Graffiti | Chess King (SWAT Blue)",
"Sealed Graffiti | Cocky (Violent Violet)",
"Sealed Graffiti | Eat It (Cash Green)",
"Sealed Graffiti | Eat It (Tiger Orange)",
"Sealed Graffiti | Eco (Battle Green)",
"Sealed Graffiti | Eco (Brick Red)",
"Sealed Graffiti | GLHF (Cash Green)",
"Sealed Graffiti | Heart (Monster Purple)",
"Sealed Graffiti | Jump Shot (Monster Purple)",
"Sealed Graffiti | Karambit (Blood Red)",
"Sealed Graffiti | Karambit (Tiger Orange)",
"Sealed Graffiti | Keep the Change (Battle Green)",
"Sealed Graffiti | Keep the Change (Frog Green)",
"Sealed Graffiti | King Me (Blood Red)",
"Sealed Graffiti | King Me (Jungle Green)",
"Sealed Graffiti | King Me (War Pig Pink)",
"Sealed Graffiti | Mr. Teeth (Jungle Green)",
"Sealed Graffiti | NaCl (Shark White)",
"Sealed Graffiti | Ninja (SWAT Blue)",
"Sealed Graffiti | Noscope (Bazooka Pink)",
"Sealed Graffiti | Popdog (War Pig Pink)",
"Sealed Graffiti | QQ (Princess Pink)",
"Sealed Graffiti | Quickdraw (Jungle Green)",
"Sealed Graffiti | Sheriff (Violent Violet)",
"Sealed Graffiti | Speechless (Jungle Green)",
"Sealed Graffiti | Speechless (SWAT Blue)",
"Sealed Graffiti | Still Happy (Bazooka Pink)",
"Sealed Graffiti | Still Happy (War Pig Pink)",
"Sealed Graffiti | Take Flight (Brick Red)",
"Sealed Graffiti | Take Flight (Dust Brown)",
"Sealed Graffiti | Tilt (Brick Red)",
"Sealed Graffiti | Tilt (War Pig Pink)",
"Sealed Graffiti | Toasted (Desert Amber)",
"Sealed Graffiti | Toasted (Dust Brown)",
"Sealed Graffiti | Tombstone (Desert Amber)",
"Sealed Graffiti | Worry (Blood Red)",
"Sealed Graffiti | Worry (Princess Pink)",
"Sealed Graffiti | X-Axes (Battle Green)",
"Sealed Graffiti | X-Axes (Bazooka Pink)",
"Sealed Graffiti | X-Axes (Monster Purple)"
]
This list can be modified or extended based on specific requirements. Ensure that each item name accurately matches the naming conventions used in the Steam Community Market to guarantee correct data retrieval.
3. Constructing the API URL
For each item, the script constructs a URL tailored to the Steam Community Market API endpoint. Proper URL encoding is essential to handle special characters and spaces in item names:
base_url = "https://steamcommunity.com/market/priceoverview/"
appid = '730' # Counter-Strike: Global Offensive app ID
currency = '3' # Currency code (3 for EUR)
The market_hash_name parameter in the URL is derived from the item name, ensuring it's correctly formatted using URL encoding:
formatted_name = quote(item_name)
The complete URL structure is as follows:
url = f"{base_url}?appid={appid}¤cy={currency}&market_hash_name={formatted_name}"
4. Fetching the Lowest Price
The core functionality involves sending a GET request to the constructed URL and parsing the JSON response to extract the lowest_price:
try:
# Introduce a delay to respect rate limiting
time.sleep(3)
# Send GET request to the API
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if data and 'lowest_price' in data:
# Clean and convert the price to a float value
price_str = data['lowest_price'].replace('€', '').replace(',', '.').strip()
lowest_price = float(price_str)
else:
lowest_price = None
else:
print(f"Failed to fetch data for {item_name}. HTTP Status Code: {response.status_code}")
lowest_price = None
except Exception as e:
print(f"Error fetching price for {item_name}: {e}")
lowest_price = None
This section ensures that the script gracefully handles scenarios where the API might not return the expected data or encounters network issues.
5. Recording Data into Excel
Using the pandas library, the script organizes the fetched data into a structured format and exports it to an Excel file:
# Initialize a dictionary to store results
results = {'Item Name': [], 'Lowest Price': []}
# Iterate through each item and fetch its lowest price
for item in items:
print(f"Processing: {item}")
price = get_item_price(item)
results['Item Name'].append(item)
results['Lowest Price'].append(price)
# Create a DataFrame from the results
df = pd.DataFrame(results)
# Export the DataFrame to an Excel file
excel_file = 'CSGO_Item_Lowest_Prices.xlsx'
df.to_excel(excel_file, index=False)
print(f"\nPrices have been successfully saved to {excel_file}")
The resulting Excel file, CSGO_Item_Lowest_Prices.xlsx, will contain two columns:
- Item Name: The name of the Counter-Strike item.
- Lowest Price: The lowest available price for the item as retrieved from the Steam Community Market.
6. Complete Script
Combining all the components, the complete Python script is as follows:
import requests
import time
import pandas as pd
from urllib.parse import quote
def get_item_price(item_name):
base_url = "https://steamcommunity.com/market/priceoverview/"
appid = '730' # Counter-Strike: Global Offensive app ID
currency = '3' # Currency code (3 for EUR)
formatted_name = quote(item_name)
url = f"{base_url}?appid={appid}¤cy={currency}&market_hash_name={formatted_name}"
try:
# Introduce a delay to respect rate limiting
time.sleep(3)
# Send GET request to the API
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if data and 'lowest_price' in data:
# Clean and convert the price to a float value
price_str = data['lowest_price'].replace('€', '').replace(',', '.').strip()
lowest_price = float(price_str)
return lowest_price
else:
print(f"No price data found for {item_name}.")
return None
else:
print(f"Failed to fetch data for {item_name}. HTTP Status Code: {response.status_code}")
return None
except Exception as e:
print(f"Error fetching price for {item_name}: {e}")
return None
def main():
# List of items to fetch prices for
items = [
"Sealed Graffiti | 8-Ball (Bazooka Pink)",
"Sealed Graffiti | 8-Ball (Cash Green)",
"Sealed Graffiti | Bling (Blood Red)",
"Sealed Graffiti | Bling (Monarch Blue)",
"Sealed Graffiti | Bling (Monster Purple)",
"Sealed Graffiti | Chess King (SWAT Blue)",
"Sealed Graffiti | Cocky (Violent Violet)",
"Sealed Graffiti | Eat It (Cash Green)",
"Sealed Graffiti | Eat It (Tiger Orange)",
"Sealed Graffiti | Eco (Battle Green)",
"Sealed Graffiti | Eco (Brick Red)",
"Sealed Graffiti | GLHF (Cash Green)",
"Sealed Graffiti | Heart (Monster Purple)",
"Sealed Graffiti | Jump Shot (Monster Purple)",
"Sealed Graffiti | Karambit (Blood Red)",
"Sealed Graffiti | Karambit (Tiger Orange)",
"Sealed Graffiti | Keep the Change (Battle Green)",
"Sealed Graffiti | Keep the Change (Frog Green)",
"Sealed Graffiti | King Me (Blood Red)",
"Sealed Graffiti | King Me (Jungle Green)",
"Sealed Graffiti | King Me (War Pig Pink)",
"Sealed Graffiti | Mr. Teeth (Jungle Green)",
"Sealed Graffiti | NaCl (Shark White)",
"Sealed Graffiti | Ninja (SWAT Blue)",
"Sealed Graffiti | Noscope (Bazooka Pink)",
"Sealed Graffiti | Popdog (War Pig Pink)",
"Sealed Graffiti | QQ (Princess Pink)",
"Sealed Graffiti | Quickdraw (Jungle Green)",
"Sealed Graffiti | Sheriff (Violent Violet)",
"Sealed Graffiti | Speechless (Jungle Green)",
"Sealed Graffiti | Speechless (SWAT Blue)",
"Sealed Graffiti | Still Happy (Bazooka Pink)",
"Sealed Graffiti | Still Happy (War Pig Pink)",
"Sealed Graffiti | Take Flight (Brick Red)",
"Sealed Graffiti | Take Flight (Dust Brown)",
"Sealed Graffiti | Tilt (Brick Red)",
"Sealed Graffiti | Tilt (War Pig Pink)",
"Sealed Graffiti | Toasted (Desert Amber)",
"Sealed Graffiti | Toasted (Dust Brown)",
"Sealed Graffiti | Tombstone (Desert Amber)",
"Sealed Graffiti | Worry (Blood Red)",
"Sealed Graffiti | Worry (Princess Pink)",
"Sealed Graffiti | X-Axes (Battle Green)",
"Sealed Graffiti | X-Axes (Bazooka Pink)",
"Sealed Graffiti | X-Axes (Monster Purple)"
]
# Initialize a dictionary to store results
results = {'Item Name': [], 'Lowest Price (€)': []}
print("Fetching prices...")
# Iterate through each item and fetch its lowest price
for item in items:
print(f"Processing: {item}")
price = get_item_price(item)
results['Item Name'].append(item)
if price is not None:
results['Lowest Price (€)'].append(price)
else:
results['Lowest Price (€)'].append("Price Not Found")
# Create a DataFrame from the results
df = pd.DataFrame(results)
# Export the DataFrame to an Excel file
excel_file = 'CSGO_Item_Lowest_Prices.xlsx'
df.to_excel(excel_file, index=False)
print(f"\nPrices have been successfully saved to {excel_file}")
if __name__ == "__main__":
main()
7. Running the Script
To execute the script:
Ensure all prerequisites are met, including Python and the necessary libraries.
Copy the complete script into a Python file, for example,
fetch_cs_prices.py.Open your terminal or command prompt, navigate to the directory containing the script, and run:
python fetch_cs_prices.py
Upon execution, the script will process each item, fetch its lowest price, and record the data into the specified Excel file. Progress and any encountered errors will be displayed in the terminal.
8. Understanding the Output
The resulting Excel file, CSGO_Item_Lowest_Prices.xlsx, will be structured as follows:
| Item Name | Lowest Price (€) |
|---|---|
| Sealed Graffiti | 8-Ball (Bazooka Pink) | €1.50 |
| Sealed Graffiti | 8-Ball (Cash Green) | €2.00 |
Each row corresponds to an item from the predefined list, with its latest lowest price fetched from the Steam Community Market. If the script fails to retrieve a price for a particular item, "Price Not Found" will be displayed in the respective cell.
9. Best Practices and Tips
- Respect Rate Limits: The script includes a 3-second delay between API requests to prevent overwhelming the Steam servers and to avoid being rate-limited or blocked.
- Regular Updates: Item prices can change rapidly. Schedule the script to run at regular intervals (e.g., hourly or daily) using task schedulers like cron on Unix-based systems or Task Scheduler on Windows.
- Error Logging: Enhance the script by implementing detailed logging mechanisms to record errors and successful fetches for future reference.
- Extensibility: Modify the script to fetch additional data points, such as average price or volume of sales, to gain deeper insights into the market trends.
- Data Visualization: Utilize the recorded data to create visual representations (charts and graphs) in Excel, aiding in better understanding of price movements.
10. Potential Enhancements
While the current script effectively retrieves and records the lowest prices, consider the following enhancements for a more robust solution:
-
Multithreading: Implement multithreading to parallelize API requests, reducing the total execution time. Ensure that rate limits are still respected to avoid blocking.
-
User Interface: Develop a simple graphical user interface (GUI) to allow users to select items and configure settings without modifying the script directly.
-
Database Integration: Instead of exporting to Excel, integrate the script with a database (e.g., SQLite, PostgreSQL) for better data management and querying capabilities.
-
Automated Notifications: Set up email or messaging notifications to alert users when specific items reach desired price thresholds.
Conclusion
Automating the retrieval and recording of Counter-Strike item prices can significantly streamline the process of tracking market trends and making informed trading decisions. This comprehensive Python script provides a solid foundation for such automation, ensuring data accuracy and reliability. By adhering to best practices and considering potential enhancements, users can tailor the script to meet their specific needs and scale it as required.
References
Last updated January 19, 2025