-1

So for Personal Training I am trying to create a Highscore lookup for a game. My current code is:

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

I am trying to get the integer of the skill. A line in the response that holds the skill level looks like the following:

<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>

The 2,715 is the part that I want to log.

Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
MrMime
  • 1
  • 2

3 Answers3

1

If the web page is valid XML (it isn't always) you can load it into an XML document and find the data you need using XPath.

If it isn't valid XML, you will need to load the HTML into an HTML parser such as the HTML Agility Pack.

Or, as a cheap solution, you could just search the string for services.runescape.com/m=hiscore/a=13/ranking?category_type=0&amp;table=0&amp;page=1"> and take the characters immediately after, using something like IndexOf and Substring.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • 2
    Just use HTML Agility Pack. Relying on a web page to conform to valid XML is a sure fire way to endless headaches. – Tim Feb 06 '17 at 23:49
  • I have never worked with this how would this work? Is there an example? – MrMime Feb 07 '17 at 00:54
  • You can probably adapt [this solution](http://stackoverflow.com/questions/42076207/c-sharp-htmlagilitypack-get-content-from-all-div-with-given-class) to your needs. – John Wu Feb 07 '17 at 00:58
  • The thing is I am not loading it from a HTML file. I will be threading lots of usernames that will be checking the stats on. – MrMime Feb 07 '17 at 01:14
  • Right. So use `LoadHTML(string html)` instead of `Load(string file)`. – John Wu Feb 07 '17 at 01:49
0

There is a packet which could help you

it's a Linq to html. similar to Linq to Xml i think this could help you.

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

-1

You can do some Jquery Ajax and filter data if needed.

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

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