Valve has released a web api which provides a flexible way to query your items from outside the game. First, grab an api key by following the instructions at http://steamcommunity.com/dev.
Next, in your script, fetch http://api.steampowered.com/IEconItems_440/GetPlayerItems/v0001/?key=API_KEY&steamid=STEAMID where API_KEY and STEAMID are your api key and 64-bit steam id respectively. This returns a JSON file which contains a list of all the items in your inventory. Just grab the size of the items array.