How can i slice my data to seperate lists?

I need take some data from url but inside of div have 3 a element. And i need slice it and take it to seperate list. In Url html data is like :

            <div class="nesne row nobetciDiv ">
              <div class="col-md-4 tablo yuksek">
                  <div class="hucre hucre-ortala">
                        <a href="tel:0242 237 67 22">ARAT ECZANESİ</a>  // I need take it to first list.
                          <br>
                         <a href="tel:0242 237 67 22">0(242) 237-67-22</a> // I need take it to second list.
                     </div>
                </div>
            
            
          <div class="col-md-8 tablo yuksek">
             <div class="hucre hucre-ortala">
                 <a href="https://maps.google.com/maps?q=36.8905816274,30.6800764847" class="nadres" target="_blank">
    <img src="/Resim/Upload/mapi.png" class="mapi">
 K.KARABEKIR CD.EGITIM  ARASTIRMA HASTANESI ACIL KARSISI </a> // I need take it to third list.
                 </div>
            </div> 
          </div>

I tried do like :

List<string> pharmacyName = new List<string>();
            List<string> pharmacyAdress = new List<string>();
            List<string> pharmacyNumber = new List<string>();
            Uri url = new Uri("https://www.antalyaeo.org.tr/tr/nobetci-eczaneler");

            WebClient client = new WebClient();
            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
            string html = client.DownloadString(url);

            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(html);
            var xpath = "//text()[not(normalize-space())]";
            var emptyNodes = document.DocumentNode.SelectNodes(xpath);

             
            foreach (HtmlNode emptyNode in emptyNodes)
            {
                emptyNode.ParentNode
                         .ReplaceChild(HtmlTextNode.CreateNode(""), emptyNode);
            }
             

            HtmlNodeCollection title = document.DocumentNode.SelectNodes("//div[contains(@class,'nobetciDiv')]");

            foreach (var item in title)
            {
                HtmlNode x = item.SelectSingleNode("//div[contains(@class,'col-md-4')]");
                HtmlNode a = x.SelectSingleNode("//a");
                pharmacyName.Add(a.InnerText); 
// Its giving me like : ARAT ECZANESİ0(242) 237-67-22  Cant seperate it or cant take third one.
            }

Sorry for my bad english i tried describe with codes mostly. So thanks for all helps!

here is solutions:

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Solution 1

Firstly,<div class="col-md-4 tablo yuksek"></div> has two <a></a>,so you cannot use SelectSingleNode.Then the third <a></a> is in <div class="col-md-8 tablo yuksek"></div> rather than <div class="col-md-4 tablo yuksek"></div>.Try to change

foreach (var item in title)
            {
                HtmlNode x = item.SelectSingleNode("//div[contains(@class,'col-md-4')]");
                HtmlNode a = x.SelectSingleNode("//a");
                pharmacyName.Add(a.InnerText); 
// Its giving me like : ARAT ECZANESİ0(242) 237-67-22  Cant seperate it or cant take third one.
            }

to

foreach (var item in title)
        {
            HtmlNode x = item.SelectSingleNode("//div[contains(@class,'col-md-4')]").FirstChild;
            pharmacyName.Add(x.SelectNodes("//a[@href]")[0].InnerText);
            pharmacyAdress.Add(x.SelectNodes("a[@href]")[1].InnerText);
            string s3= item.SelectSingleNode("//div[contains(@class,'col-md-8')]").SelectSingleNode("//a[contains(@class,'nadres')]").InnerText.Replace("\r\n","").Trim();
            pharmacyNumber.Add(s3);
           
        }

result:
How can i slice my data to seperate lists?

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply