Get Google query Json with Webbrowser C#

Asked

Viewed 314 times

1

I need to perform the following process, perform a google search and get the search information in Json format, how could I do this?

I tried to do it this way, I used Webbrowser from C# and I navigated the site www.google.com, until then it worked out, only I need to get this information as Title and Link of the results of this search, someone knows the best way to do it?

NOTE: I can’t use the google API, I need to do it by hand.

WebBrowser

    private void btnPesquisar_Click(object sender, EventArgs e)
    {
        webBrowser.Navigate("www.google.com/search?q=" + txtPesquisa.Text);

        var document = webBrowser.Document;
    }
  • You can use the API google for that.

  • See if it helps you my answer

  • The google API is limited to how many requests you make, after that you get paid.. I liked the reply from @Rovannlinhalis hahah +1.

  • hehe thanks =]

  • @Rovannlinhalis, your answer helped me yes, now I’m implementing it. Thanks for the help.

  • @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?

  • 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

  • @Rovannlinhalis, perfectly, thank you very much for your help.

  • @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?

  • would have to see your code, but I imagine it’s for the event webBrowser1_DocumentCompleted that may not be being fired

  • How do I post my code, I show you here?

  • @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.

  • Give me the code so I can see

  • How do I send you my code ? it doesn’t fit here, and editing the post would ruin the post, no?

  • I guess we can chat

Show 11 more comments

1 answer

1


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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.