【问题标题】:C# Trying to read a page using XmlNodeC# 尝试使用 XmlNode 读取页面
【发布时间】:2015-11-08 02:23:46
【问题描述】:

所以我试图从最低价格到最高价格阅读 Steam 商店页面。我有所需的 URL,并且我编写了一些过去有效但不再有效的代码。我花了几天时间试图解决这个问题,但我似乎无法找到问题所在。

Link I am trying to read.

这里是代码。

    //List of items from the Steam market from lowest to highest
    private void priceFromMarket(int StartPage)
    {
        if (valueList.Count != 0)
        {
            valueList.Clear();
            numList.Clear();
            nameList.Clear();
        }
        string pageContent = null;
        string results_html = null;
        try
        {
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://steamcommunity.com/market/search/render/?query=appid:730&start=" + StartPage.ToString() + "&sort_column=price&sort_dir=asc&count=100&currency=1&l=english");
            HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse();
            using (StreamReader sr = new StreamReader(myRes.GetResponseStream()))
            {
                pageContent = sr.ReadToEnd();
            }
        }
        catch { Thread.Sleep(30000); priceFromMarket(StartPage); }
        if (pageContent == null) { priceFromMarket(StartPage); }
        try
        {
            JObject user = JObject.Parse(pageContent);
            bool success = (bool)user["success"];
            if (success)
            {
                results_html = (string)user["results_html"];
                string data = results_html;
                data = "<root>" + data + "</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)
                {
                    //This does not work anymore!
                    //The try fails here at line 574!
                    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
                }
            }
            else { Thread.Sleep(60000); priceFromMarket(StartPage); }
        }
        catch { Thread.Sleep(60000); priceFromMarket(StartPage); }
    }

【问题讨论】:

    标签: c# html steam


    【解决方案1】:

    将 HTML 解析为 XML 永远不可靠,因为 HTML 不必经过良好的格式化才能正确解析...

    为了在 C# 中解析 HTML,我更喜欢使用 CSQuery https://www.nuget.org/packages/CsQuery/

    它可以让您在 c# 中解析 HTML,类似于通过 jquery 进行解析。

    另一种方法是 HTML Agility Pack,您可以在不更改大部分代码的情况下使用它。它的功能类似于 System.Xml.XmlDocument 库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      相关资源
      最近更新 更多