1

I don't know what is wrong with this code. I can't see result in textBox. Somebody can check if the XPath is correct? And where is mistake??

namespace Pogoda { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public static bool TryGetTemperature(HtmlAgilityPack.HtmlDocument doc, out int temperature) { temperature = 0;

        var temp = doc.DocumentNode.SelectSingleNode(
            "//div(@class, 'temperature')]/div[contains(@class, 'temp')]");
        return temp != null ?
            int.TryParse(temp.InnerText.TrimEnd('°'), out temperature) :
            false;
    }
  
    private void btnOnet_Click(object sender, EventArgs e)
    {
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        HtmlWeb web = new HtmlWeb();
        doc = web.Load("https://pogoda.onet.pl");
        if (TryGetTemperature(doc, out int temperature))
        {
            onet.Text = temperature.ToString();
        }
        else
        {
            MessageBox.Show("Error");
        }
    }


   
}

}

  • 2
    Does this answer your question? [Scrape data from div in Windows.Form](https://stackoverflow.com/questions/71967403/scrape-data-from-div-in-windows-form) – Victor Apr 22 '22 at 18:07

0 Answers0