Go through a site's response tags C #

1

I am doing a query in google by C # and I need to get the query return the Query Title and the Query Link returned.

I got my return as follows:

public class GoogleSearch
{
    private string _TituloPesquisa;
    private string _LinkPesquisa;
    private List<GoogleResultado> _GoogleResultadoList;

    static AutoResetEvent aguardarDocumentCompleted = new AutoResetEvent(true);

    public GoogleSearch()
    {
        _GoogleResultadoList = new List<GoogleResultado>();
    }

    public string GetResultJson 
    { 
        get
        {
            return _GoogleResultadoList.ToJsonSerialization<GoogleResultado>();
        }
    }

    public IEnumerable<GoogleResultado> GetListResults
    {
        get
        {
            return _GoogleResultadoList.ToList();
        }
    }

    public async Task Pesquisar(string textoConsulta)
    {
        await ExecuteSearchAsync(textoConsulta);
    }

    private async Task ExecuteSearchAsync(string textoConsulta)
    {
        string html = await GetHtmlResponse(textoConsulta);

        HtmlDocument documento = new HtmlDocument();
        documento.LoadHtml(html);

        HtmlNodeCollection allElementsWithClassG = documento.DocumentNode.SelectNodes("//div[@class=\"g\"]");

        GoogleResultado resultado;
        HtmlNode link;
        foreach (HtmlNode x in allElementsWithClassG)
        {
            link = x.Descendants("a").FirstOrDefault();
            resultado = new GoogleResultado();
            resultado.Titulo = link.InnerText;
            resultado.Url = link.Attributes["href"].Value.Replace("/url?q=", "");

            _GoogleResultadoList.Add(resultado);                
        }            
    }

    private static async Task<string> GetHtmlResponse(string textoConsulta)
    {
        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync("https://www.google.com.br/search?num=100&q=" + textoConsulta);

        var streamRetorno = new StreamReader(await response.Content.ReadAsStreamAsync());
        return streamRetorno.ReadToEnd();             
    }                                           
}           
  

Run call:

    private void btnPesquisar_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtPesquisa.Text))
            if (MessageBox.Show("Texto para pesquisa não informado.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.OK)
                return;

        google.Pesquisar(txtPesquisa.Text);
        var stringJson = google.GetResultJson;
        var listresult = google.GetListResults;
    }

I have all search results there, but as I go through the tags to get the information, can anyone help me?

  

Return - this is the return string, but there is only the beginning, because as it is a google search the string is quite large.   

    
asked by anonymous 14.07.2017 / 21:09

1 answer

2

You will need the HtmlAgilityPack that can be added as a reference in your project by Nuget :

Once you have done this, we follow the same logic as WebBrowser, only changing some of the component's peculiarities:

Follow the code:

    private static async Task ExecuteSearchAsync(string textoConsulta)
    {
        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync("https://www.google.com.br/search?num=100&q=" + textoConsulta);

        var streamRetorno = new StreamReader(await response.Content.ReadAsStreamAsync());
        string html =  streamRetorno.ReadToEnd();

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(html);

        HtmlAgilityPack.HtmlNodeCollection allElementsWithClassG = doc.DocumentNode.SelectNodes("//div[@class=\"g\"]");

        List<Resultados> resultados = new List<Resultados>();
        Resultados r;
        HtmlAgilityPack.HtmlNode link;
        foreach (HtmlAgilityPack.HtmlNode x in allElementsWithClassG)
        {
            link = x.Descendants("a").FirstOrDefault();
            r = new Resultados();
            r.Titulo = link.InnerText;
            r.Url = link.Attributes["href"].Value.Replace("/url?q=", "");
            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; }
    }
    
14.07.2017 / 21:33