how to extract hyperllinks from the gridview

mmalik15Asked:
Who is Participating?
 
nishant joshiConnect With a Mentor Technology Development ConsultantCommented:
i have chaged code using getElementById method please check it is working fine or not.


Regards,
nishant
HtmlDocument ht = new HtmlDocument();
        HtmlAgilityPack.HtmlWeb web = new HtmlWeb();
        List<string> hrefTags = new List<string>();

        for (int i = 0; i <= 1240; i = i + 10)
        {

            HtmlAgilityPack.HtmlDocument doc = web.Load("http://cancerhelp.cancerresearchuk.org/trials/search-results/%20?SearchQuery=and%28crdtype%3astring%28%22ClinicalTrial%22%29%29and%28or%28crtrialstatus%3astring%28%22Closed%22%29%2ccrtrialstatus%3astring%28%22Open%22%29%2ccrtrialstatus%3astring%28%22Results%22%29%29%29&AdvancedSearchFormType=research_and_trials_adv_search_form&OffsetValue=" + i);
            HtmlAgilityPack.HtmlNode div = doc.GetElementbyId("content");
            foreach (HtmlNode link in div.SelectNodes("//ol[@class=\"searchresults\"]//a[@href]"))
            {
                HtmlAttribute att = link.Attributes["href"];
                hrefTags.Add(att.Value);
            }
        }

        if (File.Exists(@"C:\inetpub\wwwroot\Demo\TextFile.txt"))
        {
            try
            {
                StreamWriter output = new StreamWriter(@"C:\inetpub\wwwroot\Demo\TextFile.txt");
                foreach (string Txt in hrefTags)
                {
                    output.WriteLine(Txt.ToString());
                }
                output.Close();
            }
            catch (System.IO.IOException exc)
            {
                string st = exc.Message;
            }

        }

Open in new window

0
 
Kiran SonawaneProject LeadCommented:
You can do it easily with jquery.
Like below


$(document).ready(function(){
  $(".searchresults a").each(function(){
  alert($(this).text());
  alert($(this).attr('href'));
});
});
0
 
Cenjoy100Commented:
You can  set allowpaging option to false.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
mmalik15Author Commented:
thanks for the comments

I can't see any option to set allowpaging to false. Could you point exactly where this option is available.  Its a third party website which I need to crawl.
0
 
Cenjoy100Commented:
Sorry
I misunderstood the question.
0
 
mmalik15Author Commented:
sonawanekiran: I know just the basics of jQuery. Can you please give me an example where to write that JQuery code to get all the links
0
 
nishant joshiTechnology Development ConsultantCommented:
you have to add row command event for specific 1 row using below code..

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow dr = (GridViewRow)sender;
        HyperLink h=(HyperLink) dr.FindControl("Hyperlink1");
    }

Open in new window



and for all rows in grid view


 
foreach (GridViewRow row in GridView1.Rows)
        {
            HyperLink hr=(HyperLink)row.FindControl("HyperLInk1");
        }

Open in new window


am i in correct direction or not???

Regards,
nishant
0
 
mmalik15Author Commented:
sorry nishantcomp2512: its a thrid party website and I need hyperlinks from that website.
0
 
nishant joshiTechnology Development ConsultantCommented:
sry mmalik i am having 4.0 so not able to help wait for other experts to raise


thanks,
nishant
0
 
nishant joshiTechnology Development ConsultantCommented:
few questions:
1)want to store links i database of just want to dispaly it on client side.
2)and if website contains iframe of our link then it will be fine or not?


0
 
nishant joshiTechnology Development ConsultantCommented:
37383550 is wrong comment please ignore it.thanks.
0
 
mmalik15Author Commented:
Thanks for the comments. I manage to extract the link using htmlagaility pack
HtmlAgilityPack.HtmlWeb web = new HtmlWeb();
        List<string> hrefTags = new List<string>();

        for (int i = 0; i <= 1240; i = i + 10)
        {

            HtmlAgilityPack.HtmlDocument doc = web.Load("http://cancerhelp.cancerresearchuk.org/trials/search-results/%20?SearchQuery=and%28crdtype%3astring%28%22ClinicalTrial%22%29%29and%28or%28crtrialstatus%3astring%28%22Closed%22%29%2ccrtrialstatus%3astring%28%22Open%22%29%2ccrtrialstatus%3astring%28%22Results%22%29%29%29&AdvancedSearchFormType=research_and_trials_adv_search_form&OffsetValue=" + i);



            foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//ol[@class=\"searchresults\"]//a[@href]"))
            {
                HtmlAttribute att = link.Attributes["href"];
                hrefTags.Add(att.Value);
            }
        }

        if (File.Exists(@"C:\inetpub\wwwroot\Demo\TextFile.txt"))
        {
            try
            {
                StreamWriter output = new StreamWriter(@"C:\inetpub\wwwroot\Demo\TextFile.txt");
                foreach (string Txt in hrefTags)
                {
                    output.WriteLine(Txt.ToString());
                }
                output.Close();
            }
            catch (System.IO.IOException exc)
            {
              string st= exc.Message;
            }

        }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.