Take information from a Website and Label it?

Asked

Viewed 517 times

1

I want to navigate to the Site and get the Current Bitcoin Value to Insert into a Label. This was my attempt that clearly didn’t work...

inserir a descrição da imagem aqui

private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("https://www.mercadobitcoin.com.br/");

            HtmlDocument doc = webBrowser1.Document;
            label2.Text = doc.GetElementById("ticker_ultimo_grande-int").OuterText;

        }

Site Area

inserir a descrição da imagem aqui

<div class="mb-yellow-text text-center relative"><div id="hugePriceBrlSymbol">R$</div>
<div id="ticker_ultimo_grande-int">3924,</div>
<div id="ticker_decimal_group" class="clearfix">
<div id="ticker_ultimo_grande-dec">61</div></div>
<div class="clearfix"></div></div>

Error message

inserir a descrição da imagem aqui

  • at the event Load the page will not have finished being loaded, very likely you will be able to pick up the value using the event private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

  • doc has value?

  • Thanks! It worked out.

3 answers

1


The error occurs because you are trying to get 'Document' before the page is loaded.

To fix just take elements on the page only after it is loaded.

To know if the page has been fully loaded use the event WebBrowser.DocumentCompleted which occurs when the control WebBrowser ends loading a document.

 private void Form1_Load(object sender, EventArgs e)
 {
     webBrowser1.Navigate("https://www.mercadobitcoin.com.br/");

 }

 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
 {
     HtmlDocument doc = webBrowser1.Document; 

     label2.Text = doc.GetElementById("ticker_ultimo_grande-int").OuterText;
 }

Image of your code working: Imagem da integração WebBrowser Winform

0

What’s happening is that you’re not giving the site time to upload to the program. I believe if you put one Thread.Sleep(3) before, work.

0

Sergio Wilker, if you’re talking about "Webcrawler" or "Webscraping" I did so to return the Commercial Dollar from a site called "The Money Converter":

using HtmlAgilityPack;

/// <summary>
/// Faz a busca do Dólar Comercial no site da The Money Converter
/// </summary>
/// <returns>Retorna Dólar Comercial</returns>
public static string RetornaDolarComercialTheMoneyConverter()
{
    string url = @"https://themoneyconverter.com/USD/BRL.aspx";
    HtmlDocument doc = new HtmlWeb().Load(url);
    HtmlNode value = doc.DocumentNode.SelectSingleNode("//*[@id='cc-ratebox']");

    string strValue = value.InnerText;
    // Precisei formatar com vírgula.
    strValue = strValue.Replace('.', ',');
    strValue = "R$ " + strValue.Substring(strValue.IndexOf("=", 0) + 2, 7);

    return strValue;
}

You need to use the browser, google for example, in the part of "More Tools" / "Developer Tools" and go looking for what you want to extract (within a DIV, for example), and using the right mouse button do a "Copy / Copy Xpath", take the exact term that will be extracted from the site (the content). Example:

// *[@id="cc-ratebox"]

I hope this is what you are looking for. If you want, I can pass a complete program...

[]'s,

Fabio I.

Browser other questions tagged

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