We all know that the result of webbrowser will be an HTML, so let’s go through the html getting the elements that are needed, the Divs, in the case of google, that have class 'g' indicating that it is a result of the search.
After having this element, just take the link, indicated by the tag and then we can get its properties.
Follows code:
Click:
webBrowser1.Navigate("https://www.google.com/search?num=100&q=carros");
Tip: Use the num parameter to get more results: search?num=100&q=[your search]
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection divs = webBrowser1.Document.GetElementsByTagName("div");
List<Resultados> resultados = new List<Resultados>();
Resultados r;
foreach (HtmlElement x in divs)
{
if (x.GetAttribute("className") == "g")
{
HtmlElement link = x.GetElementsByTagName("a")[0];
r = new Resultados();
r.Titulo = link.InnerText;
r.Url = link.GetAttribute("href");
resultados.Add(r);
}
}
int cout = resultados.Count; //Sua List com todos os resultados da pesquisa.
}
public class Resultados
{
public string Url { get; set; }
public string Titulo { get; set; }
}
Now json: I used the library Newtonsoft.Json
string json = "";
foreach (Resultado r in resultados)
json+= Newtonsoft.Json.JsonConvert.SerializeObject(r);
string seuJsonCompleto = json;
You can use the API google for that.
– gato
See if it helps you my answer
– Rovann Linhalis
The google API is limited to how many requests you make, after that you get paid.. I liked the reply from @Rovannlinhalis hahah +1.
– Marco Giovanni
hehe thanks =]
– Rovann Linhalis
@Rovannlinhalis, your answer helped me yes, now I’m implementing it. Thanks for the help.
– Nicola Bogar
@Rovannlinhalis, just takes away a doubt friend, how do you know these google result tags? 'cause in the document that Webbrowser uploads I can’t see those tags there, there’s some documentation for that?
– Nicola Bogar
no... open the search by google Chrome, and on top of one of the results right click, and then on inspect, Chrome will open the html of the page in that element, then just analyze and understand the same html
– Rovann Linhalis
@Rovannlinhalis, perfectly, thank you very much for your help.
– Nicola Bogar
@Rovannlinhalis, I’m having a problem, see if you can help me, when I did the test with your code, it worked perfectly when I drag the Webbrowser component into my form. When I managed to make it work I passed all this code to a DLL, where I will make an API for this process, ie I would pass the search string and run everything within the DLL and then get the Json object from the result, but the problem is as follows, when I use the Webbrowser created in the code the search result is empty. You know why?
– Nicola Bogar
would have to see your code, but I imagine it’s for the event
webBrowser1_DocumentCompleted
that may not be being fired– Rovann Linhalis
How do I post my code, I show you here?
– Nicola Bogar
@Rovannlinhalis, I think it’s nothing related to the event, because in tests I just put the Webbrowser component put the Url with www.google.com and then put _Webbrowser.Navigate("https://www.google.com/search?q=" + textConsult); and it brought the right result. And Now I created Webbrowser as a property and configured it like this: _Webbrowser.Url = new Uri("http://www.google.com", Urikind.Absolute); _Webbrowser.Navigate("https://www.google.com/search?q=" + textConsult); in this way it returns me nothing.
– Nicola Bogar
Give me the code so I can see
– Rovann Linhalis
How do I send you my code ? it doesn’t fit here, and editing the post would ruin the post, no?
– Nicola Bogar
I guess we can chat
– Rovann Linhalis
Let’s go continue this discussion in chat.
– Rovann Linhalis