Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the Website content in c#?

I want to read the website text without html tags and headers. i just need the text displayed in the web browser.

i don't need like this

<html>
<body>
bla bla </td><td>
bla bla 
<body>
<html>

i just need the text "bla bla bla bla".

I have used the webclient and httpwebrequest methods to get the HTML content and to split the received data but it is not possible because if i change the website the tags may change.

So is there any way to get only the displayed text in the website anagrammatically?

like image 858
Azeem Akram Avatar asked May 14 '12 07:05

Azeem Akram


2 Answers

You need to use special HTML parser. The only way to get the content of the such non regular language.

See: What is the best way to parse html in C#?

like image 112
Tigran Avatar answered Oct 05 '22 04:10

Tigran


Here is how you would do it using the HtmlAgilityPack.

First your sample HTML:

var html = "<html>\r\n<body>\r\nbla bla </td><td>\r\nbla bla \r\n<body>\r\n<html>";

Load it up (as a string in this case):

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

If getting it from the web, similar:

var web = new HtmlWeb();
var doc = web.Load(url);

Now select only text nodes with non-whitespace and trim them.

var text = doc.DocumentNode.Descendants()
              .Where(x => x.NodeType == HtmlNodeType.Text && x.InnerText.Trim().Length > 0)
              .Select(x => x.InnerText.Trim());

You can get this as a single joined string if you like:

String.Join(" ", text)

Of course this will only work for simple web pages. Anything complex will also return nodes with data you clearly don't want, such as javascript functions etc.

like image 26
yamen Avatar answered Oct 05 '22 03:10

yamen