Scraping an External Html

Asked

Viewed 206 times

0

I’m trying to get a text from another site using C# (Htmlagilitypack).

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

I believe I’m forgetting some piece of code.

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();
    }
}

Follows my HTML:

<h2>Teste</h2>
<div>
    @ViewBag.Div
</div>
  • doing this you inside your div will be like picking up a Documentnode and calling the Tostring() method on it. I think you just call the property InnerHtml: @ViewBag.Div.InnerHtml

  • @Rovannlinhalis function bears the name of the Collection. I managed to solve, I will post as an answer if anyone needs in the future. Thanks for the help!

  • It has no direct relation to your doubt, but in the case of the given example, weather, they have an API to access the data, without the need to read this HTML: http://apiadvisor.climatempo.com.br/doc/index.html

1 answer

0

I managed to solve, the function returns a Collection and I was treating it like a string, so it returned the name of Collection.

I just added a foreach to pick up the contents of Collection. Stayed 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();
    }
  • I understand, but check your code that there’s something unnecessary... if you’re looking for an element by the id, you should return only one element, not a collection. Maybe use another method instead of SelectNodes

  • 1

    It makes sense @Rovannlinhalis. I will search on the subject.

Browser other questions tagged

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