Sunday, 22 February 2015

Get The FaceBook Likes

Hello Guys,
Today I want to show you show you c# code through which you can easily get likes from a facebook page. Getting Facebook likes from a page is very easy if you know the URL of that page but if you dont know the url then you need to get search within facebook so that you get the url of that page.
Here i have used HTML agility pack to traverse through DOM of facebook page.
 Your likes will be stored in result variable.

       

  HtmlWeb htmlWeb = new HtmlWeb();
   HtmlAgilityPack.HtmlDocument htmlDocument = htmlWeb.Load(@"URL of Facebook Page");
                   
                   
 string result=null;
 // oldOne var htmlResults = htmlDocument.DocumentNode.SelectSingleNode("/html[1]/body[1]/code[1]");
 var htmlResults = htmlDocument.DocumentNode.SelectSingleNode("/html[1]/body[1]/code[3]");
 var verifiedpageNode = htmlDocument.DocumentNode.SelectSingleNode("/html[1]/body[1]/script[8]");
 int startpoint = 1;
 var lastindex = htmlResults.InnerText.LastIndexOf("https://www.facebook.com/");
 bool con = true;
 int count = 0;
         while ((startpoint != lastindex || con))
           { var index = htmlResults.InnerText.IndexOf("https://www.facebook.com/", startpoint);
              if (index < 0)
              break;
             var endpoint = htmlResults.InnerText.IndexOf("\"", index + 1);
              if (endpoint > index&& startpoint>0 )
               { htmlDocument = htmlWeb.Load((htmlResults.InnerText.Substring(index, endpoint - index)));
                 var nodes = htmlDocument.DocumentNode.Descendants().SingleOrDefault(x => x.Id == "u_0_j");
               var likes = (nodes.InnerText.Split('>')[17].Split('<').FirstOrDefault().Trim());
               if (index >= 0)
                {  result += "likes= " + likes + " " + (htmlResults.InnerText.Substring(index, endpoint - index)) + Environment.NewLine;
                } startpoint = index + 1;
                if (startpoint == lastindex + 1)
                  break; }
                        }

       
 
Previous Post
First

About Author

0 comments: