Scraping Steam screenshots with PHP

Here is how to scrape a Steam games screenshots with PHP. It’s actually very simple.

All games on Steam have a community hub which has news, videos, forums, guides, screenshots and more.

You can browse Steam screenshots organised by: Most popular (day/week/3months/6months/year/all time) or most recent. This code uses most recent, you can also append pages to the url with p= like so:

Note the number after app/ that is the game id 730 is Counter Strike Global Offensive, that’s what this code will scrape.

Here is the PHP code with comments to help you understand what each line of the code does:

$html = file_get_contents('');//CSGO most recent screenshots
$dom = new domDocument;//New DOM
@$dom->loadHTML($html);//put html into the DOM
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');//every <img tag
$x = $y = 0;
foreach ($images as $image) {
    $url = $image->getAttribute('src');//Gets the src from the img tags, src is the image url
    if (strpos($url, '/ugc/') !== false) {//Only work with image urls containing /ugc/     these are ALWAYS the screenshots
        $image_full = substr($url, 0, strpos($url, "?interpolation"));//removes everything after and including ?interpolation      this cleans the url
        $array = explode("/", $image_full);//Splits the url up in an array from the /
        if (isset($array[5])) {
            $file_id = $array[5];// gets the info in between the last / /
            file_put_contents("steam/screenshots/csgo/$file_id.jpg", file_get_contents($image_full));//save the image to the directory
            //echo "<img src='$image_full' width='300' height='210'><br>";//Show images saved
            $x++;//success counter
        } else {
            $y++;//issue counter
echo "Saved $x images, had issues with $y";

For most part the only things you need to change are the appid (game), page, screenshot organisation (set at most recent) and the directory to save the screenshots into (currently it is set to: steam/screenshots/csgo/).

Now just point a cron job at this PHP file (every minute) and you can rip screenshots.