🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# RMOS / HtmlAgilityPack ve Regex kullanımı

1-) C# RMOS - HtmlAgilityPack kullanımı

 

1-) HtmlAgilityPack href linkini alma

örnek -> <a href="https://bayikare.com/products/ac-220v-3-amper-12-v-smps-adaptor-11037"/>

 

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

 doc.LoadHtml(htmlTumVeriler);

string link= doc.DocumentNode.SelectSingleNode("//*[@id=\"tab-1\"]/div[1]/div/div[1]/div/div[1]/a").Attributes["href"].Value;

 

1.1-) bulunduğum div'in içindeki tüm hrefleri almak için ise

 

 List<string> linkler = new List<string>();

 

                var nodes = doc3.DocumentNode.SelectSingleNode("//*[@id=\"divIlgiliUrunler\"]/div/div/ul");

 

                if (nodes!=null)

                {

                    var hrefs = nodes.SelectNodes(".//a[@href]");

                    if (hrefs != null)

                    {

                        foreach (var link in hrefs)

                        {

                            string hrefValue = link.GetAttributeValue("href", string.Empty);

                            linkler.Add(hrefValue);

                        }

                    }

                }

 

1-) C# RMOS - REGEX örnek 1

/*

AŞAĞIDAKİ KODUN AÇIKLAMASI ;

ReservationSearch_GridView_ ile başlayan ve ardından sayılar gelen sonra ise _row ile bitenleri listeye at sonra bu listeyi dön demek

*/

 

 string pattern = @"ReservationSearch_GridView_[0-9]+_row";

 Regex rg = new Regex(pattern);

 MatchCollection _row = rg.Matches(result2);

 List<string> rezList = new List<string>();

 for (int count = 0; count < _row.Count; count++)

 { string id = _row[count].Value.Replace("ReservationSearch_GridView_", "").Replace("_row", ""); }

    

 

2-) REGEX örnek 2 -> aşağıdaki kod 2 den fazla boşlukları siler birden fazla boşlukları siler boşlukları teke indirir

 

public string ikidenFazlaBosluklariSil(string txt)

        {

            RegexOptions options = RegexOptions.None;

            Regex regex = new Regex(@"[ ]{2,}", options);

            txt = regex.Replace(txt, @" ");

            return txt;

        }

 

3-) HtmlAgilityPack örnek 1 -> aşağıdaki kod tüm tbody'leri dolaş demek veya table yazabilirsiz

 

            HtmlAgilityPack.HtmlDocument doc3 = new HtmlAgilityPack.HtmlDocument();

            doc3.LoadHtml(htmlText);

 

            foreach (var row in doc3.DocumentNode.SelectNodes("//*/tbody"))

            {

  string outHtml = System.Net.WebUtility.HtmlDecode(row.OuterHtml.ToString());

                string innerHTML = System.Net.WebUtility.HtmlDecode(row.InnerText.ToString());}

 

4-) HtmlAgilityPack örnek 2 -> aşağıdaki kod tüm tbody'leri n içindeki tr leri dolaş demek

 

 foreach (var row in doc3.DocumentNode.SelectNodes("//*/tbody"))

            {

 foreach (var row2 in row1.SelectNodes("./tr"))

                    {

  string outHtml = System.Net.WebUtility.HtmlDecode(row2 .OuterHtml.ToString());

                string innerHTML = System.Net.WebUtility.HtmlDecode(row2 .InnerText.ToString());}}

 

5-) HtmlAgilityPack örnek 3 -> aşağıdaki kod tüm tbody'leri n içindeki tr lerin data-row-type=customer olanların td lerini dolaş demek ->

 

 

foreach (var row in doc3.DocumentNode.SelectNodes("//*/tbody"))

            {

 foreach (var row1 in row.SelectNodes("./tr[@data-row-type=\"customer\"]"))

                  foreach (var row2 in row1.SelectNodes("./td")){

  string outHtml = System.Net.WebUtility.HtmlDecode(row2 .OuterHtml.ToString());

                string innerHTML = System.Net.WebUtility.HtmlDecode(row2 .InnerText.ToString());}}

 

örnek resim                             

6-) Regex örnek

 

            string result2 = response2.Content.ReadAsStringAsync().Result;

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

            doc.LoadHtml(result2);

 

            foreach (var row1 in doc.DocumentNode.SelectNodes("//*/td[@class=\"dxpSummary_Coral\"]"))

            {

                string innerText = row1.InnerHtml;

                string outherText = row1.OuterHtml;

 

            }

 

açıklama : "//*/td[@class=\"dxpSummary_Coral\"]"   demek tüm td leri dolaş class ismi dxpSummary_Coral olanları getir demek

 

 

 

6-) TEK DEĞER ALMA

 

 HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

  doc.LoadHtml(htmlText);

 

foreach (var row1 in doc.DocumentNode.SelectNodes("//*/tbody/tr"))

                { ...

     ..

      ..

 

 

 

 

 

 

                    1.YOL

foreach (var row2 in row1.SelectNodes("./font"))

   {

      string voucher = row2.InnerHtml.ToString();

   }

                    2.YOL

var href = row1.SelectSingleNode("./font").InnerHtml;

      3.YOL

 

 string voucher = row1.SelectSingleNode("./input").Attributes["claim"].Value;

  4.YOL

string val = navigator.SelectSingleNode("//div[@id='topslot']/a/img/@src").Value;

 

            string val = doc2.DocumentNode.SelectSingleNode("//*[@id=\"print-reservation\"]/div[1]/div[2]/div[2]").InnerText;

 

 

 

7-) Tüm table'ları dolaşır ve listeye atar create by RAMBO :)

 

 

 string result3 = System.Net.WebUtility.HtmlDecode(response3.Content.ReadAsStringAsync().Result);

 

  List<string> tables = new List<string>();

 

  while (result3.Contains("table")){

 

     string table = result3.Substring(result3.LastIndexOf("<table"), result3.Length -        result3.LastIndexOf("<table"));

 

  string table_s = table.Substring(0, table.IndexOf("</table>"))+ "</table>";

 

  result3 = result3.Replace(table_s,"");

 

   tables.Add(table_s);

 

 }

 

 

// içindekileri alma kodu aşağıdadır

 

 

 

 for (int i = 0; i < tables.Count; i++)

   {

    string degers = tables[i];

 

    HtmlAgilityPack.HtmlDocument doc3 = new HtmlAgilityPack.HtmlDocument();

    doc3.LoadHtml(degers);

 

      foreach (var row in doc3.DocumentNode.SelectNodes("/table/tr"))

          {

              foreach (var row1 in row.SelectNodes("./td"))

                   {

                        string innerText = row1.InnerText;

                        string innerHtml = row1.InnerHtml;

     string outerHtml = row1.OuterHtml;

                   }

           }

    }

 

 

5-) HtmlAgility last() = son ve Descendants() = tersten örnek kullanımı

 

   int aa = 1;

                foreach (var item in doc2.DocumentNode.SelectNodes("//*[@id=\"print-reservation\"]/table[last()]/tr/td"))

                {

                    string innerText = item.InnerText;

                    if (aa == 2)

                    {

                        misafirList_.islemYapan = innerText.Trim();

                    }

                    else if (aa == 4)

                    {

                        misafirList_.notunuz = innerText.Trim();

                        break;

                    }

                    aa++;

                }

 

                aa = 1;

                foreach (var item in doc2.DocumentNode.SelectNodes("//*[@id=\"print-reservation\"]/table/tr").Descendants())

                {

 

                    string innerText = item.InnerText;

 

                    if (aa == 5)

                    {

                        misafirList_.gonderen = innerText.Trim();

 

                    }

                    else if (aa==12)

                    {

                        misafirList_.acenteNotu = innerText.Trim();

                        break;

                    }

                    aa++;

                }

 

 

8-) class şu olan tableyi getir

 

 foreach (var item in doc.DocumentNode.SelectNodes("//table[@class=\"musteribilginew\"]"))

                {

                    foreach (var row in item.SelectNodes("./tr"))

                    {

 

                    }

                }

 

 2022 Eylül 17 Cumartesi
 1,378