Get the price of an item on Steam Community Market with PHP and Regex

前端 未结 3 1974
广开言路
广开言路 2020-12-01 10:05

I\'m trying to use PHP to get the Steam Community Market price of an item. I take a url (for example : http://steamcommunity.com/market/listings/730/StatTrak%E2%84%A2%20P250

3条回答
  •  执笔经年
    2020-12-01 10:16

    Don't use regex for this task (see RegEx match open tags except XHTML self-contained tags, but there's a more explanatory link somewhere on SO)

    You want to use XPath to select your elements based on fine criteria. From PHP.net this should get you the nodes you want:

    $doc = new DOMDocument();
    $doc->loadHTMLFile($file);
    $xpath = new DOMXpath($doc);
    
    $elements = $xpath->query('//span[@class="market_listing_price market_listing_price_with_fee"]');
    

    the XPath //span[@class="..."] means select all span tags within the document the have the expected class attribute.

提交回复
热议问题