0

How would I get the name, price and volume/amount of these listings. I need to put each in their own list afterwards. So I guess I also need some kind of for each loop

These are 3 items I can get a list with 100 items but for this example I only got 3.

<root>  <div class="market_listing_table_header">
        <div class="market_listing_price_listings_block">
            <div class="market_listing_right_cell market_listing_their_price market_sortable_column" data-sorttype="price">PRICE<span class="market_sort_arrow" style="display:none;"></span></div>
            <div class="market_listing_right_cell market_listing_num_listings market_sortable_column" data-sorttype="quantity">QUANTITY<span class="market_sort_arrow" style="display:none;"></span></div>
            <div class="market_listing_right_cell market_listing_price_listings_combined market_sortable_column" data-sorttype="price">PRICE<span class="market_sort_arrow" style="display:none;"></span></div>
        </div>
                <div class="market_sortable_column" data-sorttype="name"><span class="market_listing_header_namespacer"></span>NAME<span class="market_sort_arrow" style="display:none;"></span></div>
    </div>

<a class="market_listing_row_link" href="http://steamcommunity.com/market/listings/730/P250%20%7C%20Sand%20Dune%20%28Field-Tested%29" id="resultlink_0">
    <div class="market_listing_row market_recent_listing_row market_listing_searchresult" id="result_0">
                <img id="result_0_image" src="http://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpopujwezhz3MzbcDNG09GzkImemrmkMeuEzjwB7sMgjL_H8d6hilbs_0c5Ym6lI9WddVc2MgyB-FXtl7q6m9bi65jdplfl/62fx62f" srcset="http://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpopujwezhz3MzbcDNG09GzkImemrmkMeuEzjwB7sMgjL_H8d6hilbs_0c5Ym6lI9WddVc2MgyB-FXtl7q6m9bi65jdplfl/62fx62f 1x, http://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpopujwezhz3MzbcDNG09GzkImemrmkMeuEzjwB7sMgjL_H8d6hilbs_0c5Ym6lI9WddVc2MgyB-FXtl7q6m9bi65jdplfl/62fx62fdpx2x 2x"" style="border-color: #D2D2D2;" class="market_listing_item_img" alt="" />       
                <div class="market_listing_price_listings_block">
            <div class="market_listing_right_cell market_listing_num_listings">
                <span class="market_table_value">
                    <span class="market_listing_num_listings_qty">41,449</span>
                </span>
            </div>
            <div class="market_listing_right_cell market_listing_their_price">
                <span class="market_table_value">
                    Starting at:<br/>
                    <span style="color:white">$0.03 USD</span>
                </span>
                <span class="market_arrow_down" style="display: none"></span>
                <span class="market_arrow_up" style="display: none"></span>
            </div>
        </div>

                <div class="market_listing_item_name_block">
            <span id="result_0_name" class="market_listing_item_name" style="color: #D2D2D2;">P250 | Sand Dune (Field-Tested)</span>
            <br/>
            <span class="market_listing_game_name">Counter-Strike: Global Offensive</span>
        </div>
        <div style="clear: both"></div>
    </div>
</a>

<a class="market_listing_row_link" href="http://steamcommunity.com/market/listings/730/Operation%20Breakout%20Weapon%20Case" id="resultlink_1">
    <div class="market_listing_row market_recent_listing_row market_listing_searchresult" id="result_1">
                <img id="result_1_image" src="http://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ5KBFZv668FFMu1aPMI24auITjxteJwPXxY72AkGgIvZAniLjHpon2jlbl-kpvNjz3JJjVLFG9rl1YLQ/62fx62f" srcset="http://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ5KBFZv668FFMu1aPMI24auITjxteJwPXxY72AkGgIvZAniLjHpon2jlbl-kpvNjz3JJjVLFG9rl1YLQ/62fx62f 1x, http://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ5KBFZv668FFMu1aPMI24auITjxteJwPXxY72AkGgIvZAniLjHpon2jlbl-kpvNjz3JJjVLFG9rl1YLQ/62fx62fdpx2x 2x"" style="border-color: #D2D2D2;" class="market_listing_item_img" alt="" />     
                <div class="market_listing_price_listings_block">
            <div class="market_listing_right_cell market_listing_num_listings">
                <span class="market_table_value">
                    <span class="market_listing_num_listings_qty">2,594,132</span>
                </span>
            </div>
            <div class="market_listing_right_cell market_listing_their_price">
                <span class="market_table_value">
                    Starting at:<br/>
                    <span style="color:white">$0.03 USD</span>
                </span>
                <span class="market_arrow_down" style="display: none"></span>
                <span class="market_arrow_up" style="display: none"></span>
            </div>
        </div>

                <div class="market_listing_item_name_block">
            <span id="result_1_name" class="market_listing_item_name" style="color: #D2D2D2;">Operation Breakout Weapon Case</span>
            <br/>
            <span class="market_listing_game_name">Counter-Strike: Global Offensive</span>
        </div>
        <div style="clear: both"></div>
    </div>
</a>

<a class="market_listing_row_link" href="http://steamcommunity.com/market/listings/730/MP7%20%7C%20Army%20Recon%20%28Field-Tested%29" id="resultlink_2">
    <div class="market_listing_row market_recent_listing_row market_listing_searchresult" id="result_2">
                <img id="result_2_image" src="http://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpou6ryFBRw7ODYYzxb092kmZm0mvLwOq7c2GgAuMEpjLqY89mt3wzsr0RpZDygI4PAdQ42MgrS81bqk7u818C8tcjXiSw08qBOp-g/62fx62f" srcset="http://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpou6ryFBRw7ODYYzxb092kmZm0mvLwOq7c2GgAuMEpjLqY89mt3wzsr0RpZDygI4PAdQ42MgrS81bqk7u818C8tcjXiSw08qBOp-g/62fx62f 1x, http://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpou6ryFBRw7ODYYzxb092kmZm0mvLwOq7c2GgAuMEpjLqY89mt3wzsr0RpZDygI4PAdQ42MgrS81bqk7u818C8tcjXiSw08qBOp-g/62fx62fdpx2x 2x"" style="border-color: #D2D2D2;" class="market_listing_item_img" alt="" />      
                <div class="market_listing_price_listings_block">
            <div class="market_listing_right_cell market_listing_num_listings">
                <span class="market_table_value">
                    <span class="market_listing_num_listings_qty">53,539</span>
                </span>
            </div>
            <div class="market_listing_right_cell market_listing_their_price">
                <span class="market_table_value">
                    Starting at:<br/>
                    <span style="color:white">$0.03 USD</span>
                </span>
                <span class="market_arrow_down" style="display: none"></span>
                <span class="market_arrow_up" style="display: none"></span>
            </div>
        </div>

                <div class="market_listing_item_name_block">
            <span id="result_2_name" class="market_listing_item_name" style="color: #D2D2D2;">MP7 | Army Recon (Field-Tested)</span>
            <br/>
            <span class="market_listing_game_name">Counter-Strike: Global Offensive</span>
        </div>
        <div style="clear: both"></div>
    </div>
</a>
</root>

I have tried this and cant come up with any other way.

                XmlDocument document = new XmlDocument();
                document.LoadXml(System.Net.WebUtility.HtmlDecode(data));
                XmlNode rootnode = document.SelectSingleNode("root");
                XmlNodeList items = rootnode.SelectNodes("./a/div");
                foreach (XmlNode node in items)
                {
                    string value = node.SelectSingleNode("./div[contains(concat(' ', @class, ' '), ' market_listing_their_price ')]/span/span").InnerText;
                    string num = node.SelectSingleNode("./div[contains(concat(' ', @class, ' '), ' market_listing_num_listings ')]/span/span").InnerText;
                    string name = node.SelectSingleNode("./div/span[contains(concat(' ', @class, ' '), ' market_listing_item_name ')]").InnerText;
                    valueList.Add(value); //Lowest price for the item
                    numList.Add(num); //Volume of that item
                    nameList.Add(name); //Name of that item
                }

Data is the data from the first box of code. It fails at document.LoadXml(System.Net.WebUtility.HtmlDecode(data)); and I can not see why and I don't know what I do wrong.

Edit: Now I have come up with this code but it does not work, any suggestions on what to do?

                HtmlDocument node = new HtmlDocument();
                node.Load(data);
                valueList = node.DocumentNode.SelectNodes("./div[contains(concat(' ', @class, ' '), ' market_listing_their_price ')]/span/span")
                                .Select(p => p.InnerText)
                                .ToList();
                numList = node.DocumentNode.SelectNodes("./div[contains(concat(' ', @class, ' '), ' market_listing_num_listings ')]/span/span")
                                .Select(p => p.InnerText)
                                .ToList();
                nameList = node.DocumentNode.SelectNodes("./div/span[contains(concat(' ', @class, ' '), ' market_listing_item_name ')]")
                                .Select(p => p.InnerText)
                                .ToList();
                //valueList.Add(value); //Lowest price for the item
                //numList.Add(num); //Volume of that item
                //nameList.Add(name); //Name of that item
Martin T
  • 86
  • 8
  • 1
    Please show what you have tried. – dbugger Nov 09 '15 at 02:08
  • 1
    The file you are trying to load is not valid XML. Not all html is valid XML - this is why you are getting an error loading it into an XmlDocument. – John Nov 09 '15 at 02:42
  • 1
    HTML is not XML. Reading HTML is very common task and there are plenty of question about it - none of answers suggest to use XML parser - so not sure where did you come up with such code. The linked [duplicate](http://stackoverflow.com/questions/13234394/get-the-value-of-an-html-element) should get you started, if you don't like HtmlAgilityPack make sure to search for similar tools and ask clarifying questions if particular approach does not work for your case. – Alexei Levenkov Nov 09 '15 at 02:49
  • Well I made it my self, but thanks for the link and explanation! :D – Martin T Nov 09 '15 at 04:37
  • Still cant get it to work. – Martin T Nov 09 '15 at 05:13

0 Answers0