Skip to content

Instantly share code, notes, and snippets.

@karthik20522
Created May 25, 2019 09:27
Show Gist options
  • Save karthik20522/ffa4463596da0d439abbe6b50a058058 to your computer and use it in GitHub Desktop.
Save karthik20522/ffa4463596da0d439abbe6b50a058058 to your computer and use it in GitHub Desktop.
//example: http://www.bing.com/local/default.aspx?what=Saravana&where=London
var html = new WebClient().DownloadString("http://www.bing.com/local/default.aspx?what=" + name + "&where=" + location);
var htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.OptionFixNestedTags = true;
htmlDoc.LoadHtml(html);
var nodes = htmlDoc.DocumentNode.SelectNodes("//div[@class='ecEnclosure']");
if (nodes != null)
{
foreach (HtmlNode node in nodes)
{
var bName = node.Descendants("a")
.Where(s => s.GetAttributeValue("class", "").Contains("ecHeaderLink"))
.Select(s => s.InnerText).FirstOrDefault();
var address = node.Descendants("span")
.Where(s => s.GetAttributeValue("id", "").Contains("AddressLine"))
.Select(s => s.InnerText).FirstOrDefault();
var phone = node.Descendants("span")
.Where(s => s.GetAttributeValue("class", "").Contains("ecAddress"))
.Select(s => s.InnerText).FirstOrDefault();
var website = string.Empty;
var links = node.Descendants("a")
.Where(s => s.GetAttributeValue("id", "").Contains("ohlWeb"))
.Select(s => s).FirstOrDefault();
if (links != null)
website = links.GetAttributeValue("href", "");
if (!string.IsNullOrEmpty(bName))
{
var bCard = new Business()
{
Address = address ?? string.Empty,
Name = bName ?? string.Empty,
PhoneNumber = phone ?? string.Empty,
Website = website ?? string.Empty
};
//do something with bCard
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment