Get HTML href attribute in C #

1

I have the following code in a Razor view:

<div id="HTMLreturned" class="row">

   @Html.Raw(ViewBag.HTMLreturned);

 </div>

<script>

        var nrLinks = 0;

        $(".enlacesMas > a").each(function(){

            nrLinks++;
            var link = "http://www.meu.site" + $(this).attr('href').replace("..","");

            // Agora preciso de guardar todos os link num array ou algo do género


        });


</script>

No controller put in ViewBag.HTMLreturned the result of this line:

ViewBag.HTMLreturned = WebClient.client.DownloadString(meuUrl)

How can I run the code of my view in my controller so I can save each href in a C # variable ?

Note:

The html with which jQuery works is rendered before the script is executed, so these classes are html within viewbag

    
asked by anonymous 16.03.2017 / 18:10

1 answer

4

I was able to get exactly the same result in C # by installing CsQueryLatest with NuGet, and typing the following code:

 string htmlCode = "";
        List<string> links = new List<string>();

        using (WebClient client = new WebClient())
        {
            htmlCode = client.DownloadString("https://www.boe.es/buscar/anboe.php?campo%5B0%5D=TIT&dato%5B0%5D=&operador%5B0%5D=and&campo%5B1%5D=ID_DEM&dato%5B1%5D=&operador%5B1%5D=and&campo%5B2%5D=DOC&dato%5B2%5D=&operador%5B2%5D=and&campo%5B3%5D=ID_TIP&dato%5B3%5D=&operador%5B3%5D=and&campo%5B4%5D=GEO&dato%5B4%5D=&operador%5B4%5D=and&campo%5B5%5D=DOC&dato%5B5%5D=&operador%5B6%5D=and&campo%5B6%5D=FPU&dato%5B6%5D%5B0%5D=02%2F01%2F2017&dato%5B6%5D%5B1%5D=02%2F01%2F2017&page_hits=200&sort_field%5B0%5D=FPU&sort_order%5B0%5D=desc&sort_field%5B1%5D=ref&sort_order%5B1%5D=asc&accion=Buscar");
            var query = CQ.Create(htmlCode);
            var rows = query[".enlacesMas a"];
            foreach (var row in rows)
            {
                var newLink = row.Attributes["href"];
                links.Add(newLink);
                // aqui guardo todos os link numa lista
            }

        }
    
16.03.2017 / 18:50