Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried This to get the content from a shopping website..

C#
private void DisplayNewLink(string url)
    {
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load(url);

        var results = doc.DocumentNode
            .Descendants("div")
            .Where(n => n.Attributes["class"] != null && n.Attributes["class"].Value !=null)
            .Select(n => n.InnerText.Replace("\r\n", "").Trim())
            .ToArray();
    }


now the values are inside results, i want only few values to display in front end but not getting any idea.. nedd some hepl to figure out this (if possible)

Thank you......
Posted
Updated 5-Aug-13 19:33pm
v2
Comments
Maarten Kools 6-Aug-13 4:19am    
What do you mean by "few values"? Like maybe only ten results? If so, use the Take[^] method, e.g.

var results = doc.DocumentNode
.Descendants("div")
.Where(n => n.Attributes["class"] != null && n.Attributes["class"].Value !=null)
.Select(n => n.InnerText.Replace("\r\n", "").Trim())
.Take(10) // Take the first 10 results
.ToArray();
Anjanee Kumar Singh 6-Aug-13 4:24am    
Few Values Means To pick only needed values...
Maarten Kools 6-Aug-13 4:29am    
Just put in an extra where-clause then. I cannot guess what qualifies as "needed values".
Anjanee Kumar Singh 6-Aug-13 4:45am    
actualy Sir
I am crawling a shopping website say :www.abc.com
i want to extract all the post like Product name, Price etc. for this situation i written the above code...
Maarten Kools 6-Aug-13 4:56am    
You cannot do that for a random site, you need to know what the structure looks like.

So if you crawl through all divs, you can, from there, extract the information.

For example, let's say the HTML looks like this (simplified):
<div class="product">
<div class="title">My Product</div>
<div class="price">$4.99</div>
</div>

You can crawl through like so:
var results = doc.DocumentNode.Descendants("div")
.Where(n => n.Attributes["class"] != null && String.Equals(n.Attributes["class"].Value, "product", StringComparison.InvariantCultureIgnoreCase))
.Select(n => new
{
Title = n.ChildNodes.Single(cn => cn.Attributes["class"] != null && String.Equals(cn.Attributes["class"].Value, "title", StringComparison.InvariantCultureIgnoreCase)).InnerText,
Price = n.ChildNodes.Single(cn => cn.Attributes["class"] != null && String.Equals(cn.Attributes["class"].Value, "price", StringComparison.InvariantCultureIgnoreCase)).InnerText
})
.ToArray();

But if you don't know the structure of the HTML, you can't do it.

1 solution

use foreach for this like-------
foreach(var v1 in v)
{
txt1.text=v1.value;
}
 
Share this answer
 
Comments
Anjanee Kumar Singh 6-Aug-13 5:52am    
sorry i didn't understand this code...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900