Categories: Development

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:

https://steamcommunity.com/app/730/screenshots/?p=3&browsefilter=trendsixmonths

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('https://steamcommunity.com/app/730/screenshots/?p=1&browsefilter=mostrecent');//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 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];//https://steamuserimages-a.akamaihd.net/ugc/951844356900293445/DA3030AB032C01EAE1578F553EFCF0ABC89DD6C8/ 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 "
";//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/).

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago