Show title of web tag in label c #

0

I'm trying to take the content of a page from a tag web to show it in label and I could not, I tried several ways and still can not find the solution.

The information is contained in a part of the code like this:

<h3>
<a href="http://www.ebay.com/itm/PC-DESKTOP-GAMING-COMPUTER-FISSO-HD-1TB-RAM-16GB-WINDOWS-10-INTEL-QUADCORE-/281284000821?hash=item417dd53c35:g:PToAAOSwn-tZFbUN" class="vip" title="Haz clic en el enlace para ver PC DESKTOP GAMING COMPUTER FISSO HD 1TB / RAM 16GB WINDOWS 10 INTEL QUADCORE">PC DESKTOP GAMING COMPUTER FISSO HD 1TB / RAM 16GB WINDOWS 10 INTEL QUADCORE</a>
</h3>

var HttpClient = new HttpClient();

        string  url1="";
        url1 = textBox1.Text;

        System.IO.StringReader reader = new System.IO.StringReader(url1);
        string contenido = reader.ReadToEnd();
        reader.Close();
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(contenido);

        HtmlAgilityPack.HtmlDocument doc1 = new HtmlAgilityPack.HtmlDocument();
        doc1.Load(contenido);

        //var itemList = doc.DocumentNode.SelectNodes("//div[@class='s-item__subtitle']")//this xpath selects all span tag having its class as hidden first
                          //.Select(p => p.InnerText)
                          //FirstOrDefault();



        var myDiv = doc.DocumentNode.SelectNodes("//div[@class='s-item__subtitle']").Select(p => p.InnerText).FirstOrDefault();
        var li = contenido;

        myDiv = lblmostrar.Text;
    
asked by carlos casilla 16.06.2017 в 15:33
source

1 answer

0

Using the html agility pack library you can get the content as follows:

This is a sample web page:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>titulo</title>
</head>
<body>
    <h1>Este es un ejemplo para buscar contenido en una pagina web</h1>
    <h3>Algun titulo h3</h3>
    <h3>
        <a href="http://www.ebay.com/itm/PC-DESKTOP-GAMING-COMPUTER-FISSO-HD-1TB-RAM-16GB-WINDOWS-10-INTEL-QUADCORE-/281284000821?hash=item417dd53c35:g:PToAAOSwn-tZFbUN"
            class="vip"
            title="Haz clic en el enlace para ver PC DESKTOP GAMING COMPUTER FISSO HD 1TB / RAM 16GB WINDOWS 10 INTEL QUADCORE">PC DESKTOP GAMING COMPUTER FISSO HD 1TB / RAM 16GB WINDOWS 10 INTEL QUADCORE
        </a>
    </h3>
    <p>Parrafo</p>
    <h3>Otro titulo h3</h3>
</body>
</html>

And here a console application that shows the contents of a tag nested in an h3 that has the vip class:

    using System;
    using System.Linq;
    using HtmlAgilityPack;

    namespace ConsoleFindContentHtml
    {
        class Program
        {
            static void Main(string[] args)
            {
                // From File
                var doc = new HtmlDocument();
                doc.Load("PaginaWeb.html");

                // With XPath   
                var tag = doc.DocumentNode
                    .SelectNodes("//h3/a[@class='vip']")
                    .First();

                Console.WriteLine(tag.InnerText);
                Console.ReadKey();
            }
        }
    }

As a result, it shows on the screen:

PC DESKTOP GAMING COMPUTER FISSO HD 1TB / RAM 16GB WINDOWS 10 INTEL QUADCORE

    
answered by 30.06.2017 в 17:24