C#从webresponse获取文本的某些部分

时间:2017-02-06 23:16:10

标签: c#

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

Observable.create((observer: Subscriber<any>) => {
    observer.next(TodoItems);
    observer.complete();
});

我想获得技能的整数。 保持技能级别的响应中的行如下所示:

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();

2,715是我要记录的部分。

3 个答案:

答案 0 :(得分: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;table=0&amp;page=1">的字符串,然后使用IndexOfSubstring等字符立即获取字符。

答案 1 :(得分:0)

有一个可以帮助你的数据包

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

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

答案 2 :(得分:-1)

如果需要,您可以执行一些Jquery Ajax并过滤数据。

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

                },
                error:function() {
                    alert("Error");
                }
            })
        });