Scraping an External Html

0

I'm trying to get text from another site using C # (HtmlAgilityPack).

I can find the div, but when I try to display the value on the screen, it shows the path of the function.

I think I'm forgetting some code snippet.

Follow my Controller:

public class TesteDeScrapingController : Controller
{
    // GET: TesteDeScraping
    public ActionResult Index()
    {
        HtmlWeb web = new HtmlWeb();
        HtmlDocument html = web.Load("https://www.climatempo.com.br/previsao-do-tempo/cidade/583/araguaina-to");

        var div = html.DocumentNode.SelectNodes("//p[@id='tempMax0']");

        ViewBag.Div = div;

        return View();
    }
}

Follow my HTML:

<h2>Teste</h2>
<div>
    @ViewBag.Div
</div>
    
asked by anonymous 15.02.2018 / 16:56

1 answer

0

I was able to resolve, the function returns a collection and I was treating it as a string, so it returned the collection name.

I just added a foreach to get the contents of the collection. It looks like this:

public ActionResult Index()
    {
        HtmlWeb web = new HtmlWeb();
        HtmlDocument html = web.Load("https://www.climatempo.com.br/previsao-do-tempo/cidade/583/araguaina-to");

        var div = html.DocumentNode.SelectNodes("//p[@id='tempMax0']");
        var conteudo = string.Empty;

        foreach(var conteudos in div)
        {
            conteudo = conteudos.InnerHtml;
        }

        ViewBag.Div = conteudo;

        return View();
    }
    
15.02.2018 / 17:24