【问题标题】:C# Get certain part of text from a webresponseC#从webresponse获取文本的特定部分
【发布时间】:2017-02-09 04:17:54
【问题描述】:

因此,对于个人训练,我正在尝试为游戏创建高分查找。 我当前的代码是:

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://services.runescape.com/m=hiscore/a=13/compare?user1=" + textBox1.Text);
httpWebRequest.Host = "services.runescape.com";
httpWebRequest.Method = "POST";
httpWebRequest.CookieContainer = cookie;
httpWebRequest.Referer = "Referer: http://services.runescape.com/m=hiscore/a=13/ranking";
httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36";
httpWebRequest.ContentLength = (long)bytes.Length;
httpWebRequest.Headers.Add("Origin", "services.runescape.com");
httpWebRequest.Headers.Add("Cache-Control", "max-age=0");
httpWebRequest.Headers.Add("Upgrade-Insecure-Requests", "1");

Stream requestStream = httpWebRequest.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();

HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
cookie.Add(httpWebResponse.Cookies);

StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
string text = streamReader.ReadToEnd();
streamReader.Close();
httpWebResponse.Close();

我正在尝试获取技能的整数。 响应中包含技能级别的一行如下所示:

<td class="playerWinLeft alignleft"><div class="relative">a href="services.runescape.com/m=hiscore/a=13/ranking?category_type=0&amp;table=0&amp;page=1">2,715/a></div></td>

2715 是我要记录的部分。

【问题讨论】:

    标签: c#


    【解决方案1】:

    如果网页是有效的 XML(并非总是如此),您可以 load it into an XML document 并找到您需要的数据 using XPath

    如果它不是有效的 XML,则需要将 HTML 加载到 HTML 解析器中,例如 HTML Agility Pack

    或者,作为一种廉价的解决方案,您可以在字符串中搜索services.runescape.com/m=hiscore/a=13/ranking?category_type=0&amp;amp;table=0&amp;amp;page=1"&gt;,然后立即获取字符,使用类似IndexOfSubstring

    【讨论】:

    • 只需使用 HTML Agility Pack。依靠网页来符合有效的 XML 无疑是解决无休止的头痛的一种方法。
    • 我从未使用过这个,这将如何工作?有例子吗?
    • 您可以根据自己的需要调整this solution
    • 问题是我没有从 HTML 文件中加载它。我将处理大量用于检查统计信息的用户名。
    • 对。所以使用LoadHTML(string html) 而不是Load(string file)
    【解决方案2】:

    有一个包可以帮助你

    这是一个 Linq to html。 类似于 Linq to Xml 我认为这可以帮助你。

    https://bitlush.com/linq-to-html

    【讨论】:

      【解决方案3】:

      如果需要,你可以做一些 Jquery Ajax 和过滤数据。

      $(function() {
                  $.ajax({
                      type:"GET",
                      url:"somePage.html",
                      dataType:"html",
                      success:function(data) {
                          alert(data);
      
                      },
                      error:function() {
                          alert("Error");
                      }
                  })
              });
      

      【讨论】:

        猜你喜欢
        • 2016-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多