无法从网站上获取价格,名称和金额

时间:2015-11-19 11:35:19

标签: c# html list

我如何获得这些商家信息的名称,价格和数量/金额。 之后我需要将每个列表放在自己的列表中。所以我想我也需要为每个循环提供某种

这些是3项我可以获得100个项目的列表但是对于这个例子我只有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>

我已经尝试了这一点,并且无法想出任何其他方式。

                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
                }

数据是第一个代码框中的数据。 它在document.LoadXml(System.Net.WebUtility.HtmlDecode(data));失败了,我看不清楚为什么,我也不知道自己做错了什么。

编辑: 现在我已经提出了这个代码,但它没有用,有什么建议吗?

                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

1 个答案:

答案 0 :(得分:1)

这是使用正确的XPath修改的第一种方法

        var valueList = new List<string>();
        var numList = new List<string>();
        var nameList = new List<string>();
        XmlDocument document = new XmlDocument();
        document.LoadXml(System.Net.WebUtility.HtmlDecode(Resource1.New_Text_Document));
        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("//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
        }