'How to get particular items from website in C# WPF
I dont have API of website, but I want to retreive some informations from it. They look like this:
Name, Vocation and Level.
To achieve that I followed Microsoft C# Make Get Request and right now I've got full informations from Website
From this point I decided to find table:
if (sLine.StartsWith("<table width="))
{
Console.ReadLine();
}
Right now I've got whole table with names, levels etc. But its all in one line... Data right now looks like this: (but there is much more records in it)
<table width="100%" class="tabi"><tr><td colspan=7>Characters</td></tr> <tr><td height='30' style='background-color:#9f8f6d;'><a href=?page=whoisonline&ord=name&sort=DESC&id=1>↑Name</a></td><td width='240' style='background-color:#9f8f6d;'><a href=?page=whoisonline&ord=voc&sort=DESC&id=1>Vocation</a></td><td width='120' style='background-color:#9f8f6d;'><a href=?page=whoisonline&ord=lvl&sort=DESC&id=1>Level</a></td></tr><tr class='hover'> <td><a href='?page=character&name=Abe' class='menulink_hs'>Abe</a></td> <td>Elder Druid</td> <td>19</td> </tr></table><br />
so I started trimming it like this:
if (sLine.StartsWith("<table width="))
{
foreach (var a in sLine.Split("<td>"))
{
var replacementToken = "";
foreach (var b in a.Split("</td>"))
{
foreach (var c in b.Split("</tr>"))
{
foreach (var d in c.Split("<tr class='hover'>"))
{
foreach (var f in d.Split("</a>"))
{
string g = f.Replace("'", replacementToken)
.Replace("?", replacementToken)
.Replace("class=menulink_hs", replacementToken)
.Replace("a href", replacementToken)
.Replace("=page=character&", replacementToken);
//f.TrimStart('>');
//f.Trim('>');
listBox1.Items.Add(g);
}
}
}
}
}
}
and I believe there is a better solution than xxxxx foreach loops. What is the best way to get data from this table?
My full code:
string sURL;
sURL = "https://tibiantis.online/?page=whoisonline";
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);
WebProxy myProxy = new WebProxy("myproxy", 80);
myProxy.BypassProxyOnLocal = true;
Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string sLine = "";
int i = 0;
while (sLine != null)
{
i++;
sLine = objReader.ReadLine();
if (sLine != null)
{
if (sLine.StartsWith("<table width="))
{
foreach (var a in sLine.Split("<td>"))
{
var replacementToken = "";
foreach (var b in a.Split("</td>"))
{
foreach (var c in b.Split("</tr>"))
{
foreach (var d in c.Split("<tr class='hover'>"))
{
foreach (var f in d.Split("</a>"))
{
string g = f.Replace("'", replacementToken)
.Replace("?", replacementToken)
.Replace("class=menulink_hs",replacementToken)
.Replace("a href", replacementToken)
.Replace("=page=character&",replacementToken);
//f.TrimStart('>');
//f.Trim('>');
listBox1.Items.Add(g);
}
}
}
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
