public static async Task <ResultGoogle> GetGoogleSearchCustomAsync(string query) { string apiKey = "AIzaSyB6iXqwJKgIciHt1xd5P7KjoLqNf9lAGcg"; //https://console.developers.google.com/apis/credentials?project=crm103core string context = "000820817763802558027:dtcz5fl4u4y"; //https://cse.google.com/cse/setup/basic?cx=000820817763802558027%3Adtcz5fl4u4y ResultGoogle result = new ResultGoogle(); using (var client = new HttpClient()) { try { client.BaseAddress = new Uri("https://www.googleapis.com"); HttpResponseMessage fetch = await client.GetAsync($"/customsearch/v1?key={apiKey}&cx={context}&q={query}&num=10"); fetch.EnsureSuccessStatusCode(); string stringResult = await fetch.Content.ReadAsStringAsync(); GoogleCustomSearchResult response = JsonConvert.DeserializeObject <GoogleCustomSearchResult>(stringResult); foreach (Item item in response.items) { if (item.pagemap?.localbusiness != null) { if (string.IsNullOrEmpty(item.pagemap.localbusiness[0].telephone)) { continue; } result.Text = item.pagemap.localbusiness[0].telephone; int?phoneNumber = GetPhoneNumber(result.Text); if (phoneNumber != null) { result.Text = phoneNumber.ToString(); } result.Title = item.pagemap.localbusiness[0].name; result.Link = item.link; result.Status = true; result.DocumentString = JsonConvert.SerializeObject(item); return(result); } } result.DocumentString = JsonConvert.SerializeObject(response); } catch (HttpRequestException e) { Console.WriteLine(e.Message); } } result.Text = "No se ha encontrado el número"; return(result); }
public static ResultGoogle GetGoogleSearch(string query) { ResultGoogle result = new ResultGoogle(); //https://moz.com/blog/the-ultimate-guide-to-the-google-search-parameters string urlGoogle = "https://www.google.com.pe/search?q=" + query + "&hl=es-PE&cr=countryPE&gl=pe"; HtmlWeb web = new HtmlWeb(); HtmlDocument doc = web.Load(urlGoogle); result.DocumentString = doc.DocumentNode.InnerHtml; //the parameter is use xpath see: https://www.w3schools.com/xml/xml_xpath.asp HtmlNodeCollection graphGoogle = doc.DocumentNode.SelectNodes("//table[@class='O6u2Ve']"); HtmlNodeCollection divsGoogle = doc.DocumentNode.SelectNodes("//div[@class='g']"); string _title, _link, _trash, _text = string.Empty; int _length, _index = 0; int? phoneNumber; if (graphGoogle != null) { foreach (HtmlNode graph in graphGoogle) { _title = ConvertStringToUTF8(graph.ChildNodes[1].ChildNodes[0].ChildNodes[0].InnerText.Split(",".ToCharArray()).FirstOrDefault()); if (string.IsNullOrEmpty(_title)) { break; } if (graph.ChildNodes[1].ChildNodes[0].ChildNodes[1].ChildNodes.Count() > 1) { _text = graph.ChildNodes[1].ChildNodes[0].ChildNodes[1].ChildNodes[1].InnerText; } else { _text = graph.ChildNodes[1].ChildNodes[0].ChildNodes[1].InnerText; } string[] textArray = _text.Split(";".ToCharArray()); _text = textArray.FirstOrDefault(); if (textArray.Length > 1) { _text = textArray.ElementAt(1); } if (!string.IsNullOrEmpty(_text)) { phoneNumber = GetPhoneNumber(_text); if (phoneNumber != null) { result.Title = _title.Trim().Replace("amp;", ""); result.Text = phoneNumber.ToString(); result.Status = true; return(result); } } } } if (divsGoogle != null) { foreach (HtmlNode item in divsGoogle) { if (item.ChildNodes.Count == 1) { HtmlDocument innerDiv = new HtmlDocument(); innerDiv.LoadHtml(item.InnerHtml.ToString()); HtmlNodeCollection divs = innerDiv.DocumentNode.SelectNodes("//div"); if (divs[0].ChildNodes[0].ChildNodes.Count() < 2) { break; } _title = ConvertStringToUTF8(divs[0].ChildNodes[0].ChildNodes[1].InnerText.Split(",".ToCharArray()).FirstOrDefault()); _text = divs[0].ChildNodes[0].ChildNodes[0].InnerText; if (!string.IsNullOrEmpty(_text)) { phoneNumber = GetPhoneNumber(_text); if (phoneNumber != null) { result.Title = _title.Trim().Replace("amp;", ""); result.Text = phoneNumber.ToString(); result.Status = true; return(result); } } } if (item.ChildNodes.Count == 2) { _title = ConvertStringToUTF8(item.ChildNodes[0].InnerText); _link = item.ChildNodes[0].ChildNodes[0].Attributes["href"].Value; _link = _link.Replace("/url?q=", ""); _length = _link.Length; _index = _link.IndexOf("&"); _trash = _link.Substring(_index, _length - _index); _link = _link.Replace(_trash, ""); _text = ConvertStringToUTF8(item.ChildNodes[1].ChildNodes[1].InnerText); if (!string.IsNullOrEmpty(_text)) { phoneNumber = GetPhoneNumber(_text, 7, 9); //phoneNumber = GetPhoneNumber(_text); if (phoneNumber != null) { result.Title = _title.Trim().Replace("amp;", ""); result.Text = phoneNumber.ToString(); result.Link = _link; result.Status = true; return(result); } } //_result = $"Título: {_title}, Link: {_link}, Texto: {_text}"; } } } result.Text = "No se ha encontrado el número"; return(result); }