1
I’m performing a query on google by C# and I need to get back from the query the Query Title and the Link of the queries 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/search?num=100&q=" + textoConsulta);
var streamRetorno = new StreamReader(await response.Content.ReadAsStreamAsync());
return streamRetorno.ReadToEnd();
}
}
Execution 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 the search results there, but as I go through the tags to get the information, someone can help me?
Return - this is the return string, but this is only the beginning, because as it is a google search the string is very large.
That’s what I gave you yesterday ?!
– Rovann Linhalis
How is the kind of return?
– Thiago Araújo
@Rovannlinhalis, it didn’t work because when using Webbrowser the Document is generated in the asynchronous event, and I need the result before.
– Nicola Bogar
you can continue using namespace
System.Windows.Forms
?– Rovann Linhalis
@Rovannlinhalis, yes, I can, with that routine everything worked out, I only had to discard because the event of the documentCompleted is asynchronous, and I can’t pause Navigate() and soon after working with Document, it takes time to load and when loaded the method has already been executed, I have tried with Thread.Sleep Task.Delay no success has been achieved.
– Nicola Bogar
put the answer, see if it helps you
– Rovann Linhalis
still on the Webbrowser, you can put a flag and a timeout to wait for the documentCompleted, I did it once and it worked well
– Rovann Linhalis