DiscoStu
DiscoStu

Reputation: 31

Reading a textstring from a webpage

Currently I'm trying to read out a text from a Website via a c# program. To be exact the Track and the Dj from www.hardbase.fm.

This is what the page source looks like:

<div id="Moderator">
  <div id="Moderator_special">
    <div style="width:158px; float:left; margin:8px"></div>
    <div id="onAir" style="width:420px;overflow:hidden;">
      <strong>
        <a href="/member/46069" target="_top">
          <span style="color:#4AA6E5">BIOCORE</span>
        </a>
        <span style="color:#26628B"> mit "This Is BIOCORE" (Hardstyle)</span>
      </strong>
    </div>
  </div>
</div>

The text I want to read out is "BIOCORE" and "mit "This Is BIOCORE" (Hardstyle)" (the text seen when running the snippet).

If have tried the following:

System.Net.WebClient wc = new System.Net.WebClient();
byte[] raw = wc.DownloadData("http://www.hardbase.fm/");
first = webData.IndexOf("#4AA6E5\">") + "#4AA6E5\">".Length;
last = webData.LastIndexOf("</span></a><span style=\"color:#26628B\">");
hb_dj = webData.Substring(first, last - first);

But this doesn't always works because sometimes the source code of the page changes a bit. Like the color or so. And then the search wont work.

So the question is: Is there a better method to do this?

Upvotes: 3

Views: 98

Answers (1)

COLD TOLD
COLD TOLD

Reputation: 13599

You should try the HTML Agility Pack

HtmlWeb page = new HtmlWeb();
HtmlDocument document = page.Load("http://www.hardbase.fm/");

var nodes = document.DocumentNode.SelectNodes("//[@id='onAir']");
var nodes2 = nodes.Select(c1 => c1.SelectNodes("span")).ToList();

var span1=nodes2[0];
var span2 nodes2[1]

Upvotes: 3

Related Questions