[vb.net] htmlagilitypack récupérer title, metatag, hyperlink

Télécharger la librairie : htmlagilitypack

Imports System
Imports HtmlAgilityPack
 
Public Class HAP
 
    Public msgError As String
    Public isOk As Boolean
 
    Public Function initTxt(urlSource As String) As String
        msgError = ""
        Dim foo As String = ""
        If urlSource <> "" Then
            Try
                Dim web As New HtmlWeb
                Dim doc As New HtmlDocument
                doc = web.Load(urlSource)
 
                'Extract Link
                For Each titleHtml As HtmlNode In doc.DocumentNode.SelectNodes("//title")
                    foo &= titleHtml.InnerText & "<br/>"
                Next
 
                'Extract MetaTag
                For Each metaHtml As HtmlNode In doc.DocumentNode.SelectNodes("//meta")
                    If metaHtml.Attributes("name") IsNot Nothing AndAlso metaHtml.Attributes("content") IsNot Nothing AndAlso metaHtml.Attributes("name").Value = "description" Then
                        foo &= metaHtml.Attributes("content").Value & "<br />"
                    End If
 
                    If metaHtml.Attributes("name") IsNot Nothing AndAlso metaHtml.Attributes("content") IsNot Nothing AndAlso metaHtml.Attributes("name").Value = "keywords" Then
                        foo &= metaHtml.Attributes("content").Value & "<br />"
                    End If
                Next
 
                'Extract Link
                For Each linkHTML As HtmlNode In doc.DocumentNode.SelectNodes("//a")
                    Dim titleLink As String = linkHTML.InnerText
                    If titleLink = "" Then
                        titleLink = linkHTML.Attributes("title").Value
                    End If
                    foo &= (titleLink & " - " & linkHTML.Attributes("href").Value) & "<br/>"
                Next
            Catch ex As Exception
                msgError = ex.ToString()
            End Try
        End If
        Return foo
    End Function
 
End Class