0

I have an internal company's web page and I need to extract a link from that page what I tried is

using (var client = new System.Net.WebClient())
            {                    
                string pattern = @"(<a.*?>.*?</a>)";

                MatchCollection hreflist;

                string Url = client.DownloadString("https://collaborate.citi.net/docs/DOC-908807");                

                hreflist = Regex.Matches(Url, pattern);

                Console.WriteLine("Total number of links in Url: " + hreflist.Count + "\n\n");

But this code doesn't seems to work here.

1 Answer 1

0

You need to download HtmlAgilityPack

WebClient wc = new WebClient();
            var sourceCode = wc.DownloadString("http://dota-trade.com/equipment?order=name");
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(sourceCode);
            var node = doc.DocumentNode;
            var nodes = node.SelectNodes("//a");
            List<string> links = new List<string>();
            foreach (var item in nodes)
            {
                var link = item.Attributes["href"].Value;
                links.Add(link.Contains("http") ? link : "http://dota-trade.com" +link);
            }
      
  
        System.IO.File.WriteAllLines(@"C:\Users\Public\WriteLines.txt", links);
3
  • Can you please Explain me all the links that you have used in your code, as i have only one url and that i have mentioned in my question. i am bit confused with all the links that you have used. Jul 15, 2021 at 13:01
  • Code updated and simplified.@TausifKhan
    – mzonerz
    Jul 26, 2021 at 4:45
  • Thanks buddy :) Jul 27, 2021 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.