VB.net Obtenir le InnerText de href en utilisant HtmlAgilityPack

href html-agility-pack innertext vb.net

Question

J'ai maintenant mis à jour mon code (merci à Tim de m'avoir aidé à apprendre) qui fonctionne déjà mais il ne me donne pas les bons liens que je veux.

Voici mon code de travail:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim webClient As New System.Net.WebClient
        Dim WebSource As String = webClient.DownloadString("http://www.google.com.ph/search?hl=en&as_q=test&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=countryCA&as_qdr=all&as_sitesearch=&as_occt=any&safe=images&tbs=ctr%3AcountryCA&as_filetype=&as_rights=#as_qdr=all&cr=countryCA&fp=1&hl=en&lr=&q=test&start=20&tbs=ctr:countryCA")

    Dim doc = New HtmlAgilityPack.HtmlDocument()
        doc.LoadHtml(WebSource)
        Dim links = GetLinks(doc, "test")
        For Each Link In links
            ListBox1.Items.Add(Link.ToString())
        Next
    End Sub


   Public Class Link
        Public Sub New(Uri As Uri, Text As String)
            Me.Uri = Uri
            Me.Text = Text
        End Sub
        Public Property Text As String
        Public Property Uri As Uri

        Public Overrides Function ToString() As String
            Return String.Format(If(Uri Is Nothing, "", Uri.ToString()))
        End Function
    End Class


    Public Function GetLinks(doc As HtmlAgilityPack.HtmlDocument, linkContains As String) As List(Of Link)
        Dim uri As Uri = Nothing
        Dim linksOnPage = From link In doc.DocumentNode.Descendants()
                          Where link.Name = "a" _
                          AndAlso link.Attributes("href") IsNot Nothing _
                          Let text = link.InnerText.Trim()
                          Let url = link.Attributes("href").Value
                          Where url.IndexOf(linkContains, StringComparison.OrdinalIgnoreCase) >= 0 _
                          AndAlso uri.TryCreate(url, UriKind.Absolute, uri)

        Dim Uris As New List(Of Link)()
        For Each link In linksOnPage
            Uris.Add(New Link(New Uri(link.url, UriKind.Absolute), link.text))
        Next

        Return Uris
    End Function

Je suis actuellement nouveau à ce HtmlAgilityPack, je suis encore en train d'apprendre s'il vous plaît supporter avec moi.

Mon objectif principal:

Lien Exemple: http://www.google.com.ph/search?hl=en&as_q=test&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=countryCA&as_qdr=all&as_sitesearch=&as_occt=any&safe=images&tbs=ctr%3AcountryCA&as_filetype=&as_rights=#as_qdr=all&cr=countryCA&fp=1&hl=en&lr=&q=test&start=20&tbs=ctr:countryCA

Mon lien attendu contient le mot "test":

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim webClient As New System.Net.WebClient
        Dim WebSource As String = webClient.DownloadString("http://www.google.com.ph/search?hl=en&as_q=test&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=countryCA&as_qdr=all&as_sitesearch=&as_occt=any&safe=images&tbs=ctr%3AcountryCA&as_filetype=&as_rights=#as_qdr=all&cr=countryCA&fp=1&hl=en&lr=&q=test&start=20&tbs=ctr:countryCA")

    Dim doc = New HtmlAgilityPack.HtmlDocument()
        doc.LoadHtml(WebSource)
        Dim links = GetLinks(doc, "test")
        For Each Link In links
            ListBox1.Items.Add(Link.ToString())
        Next
    End Sub


   Public Class Link
        Public Sub New(Uri As Uri, Text As String)
            Me.Uri = Uri
            Me.Text = Text
        End Sub
        Public Property Text As String
        Public Property Uri As Uri

        Public Overrides Function ToString() As String
            Return String.Format(If(Uri Is Nothing, "", Uri.ToString()))
        End Function
    End Class


    Public Function GetLinks(doc As HtmlAgilityPack.HtmlDocument, linkContains As String) As List(Of Link)
        Dim uri As Uri = Nothing
        Dim linksOnPage = From link In doc.DocumentNode.Descendants()
                          Where link.Name = "a" _
                          AndAlso link.Attributes("href") IsNot Nothing _
                          Let text = link.InnerText.Trim()
                          Let url = link.Attributes("href").Value
                          Where url.IndexOf(linkContains, StringComparison.OrdinalIgnoreCase) >= 0 _
                          AndAlso uri.TryCreate(url, UriKind.Absolute, uri)

        Dim Uris As New List(Of Link)()
        For Each link In linksOnPage
            Uris.Add(New Link(New Uri(link.url, UriKind.Absolute), link.text))
        Next

        Return Uris
    End Function

Réponse acceptée

Vous devez utiliser l'attribut href place. Notez également que .NET est sensible à la casse par défaut.

For Each link As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//a[@href]")
   Dim href = link.Attributes("href").Value
   If href.IndexOf("test", StringComparison.OrdinalIgnoreCase) >= 0 Then
       ListBox1.Items.Add(href)
       ' or
       ListBox1.Items.Add(link.InnerText)
   End If
Next 

Voici une méthode qui devrait renvoyer tous les liens d'un document sous forme de List(Of Link) . Link est une classe personnalisée avec deux propriétés, une pour le texte et l'autre pour Uri :

For Each link As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//a[@href]")
   Dim href = link.Attributes("href").Value
   If href.IndexOf("test", StringComparison.OrdinalIgnoreCase) >= 0 Then
       ListBox1.Items.Add(href)
       ' or
       ListBox1.Items.Add(link.InnerText)
   End If
Next 

Voici la surcharge demandée qui vérifie si une URL contient un texte donné:

For Each link As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//a[@href]")
   Dim href = link.Attributes("href").Value
   If href.IndexOf("test", StringComparison.OrdinalIgnoreCase) >= 0 Then
       ListBox1.Items.Add(href)
       ' or
       ListBox1.Items.Add(link.InnerText)
   End If
Next 

Edité maintenant testé, fonctionne, utilisez-le de la manière suivante:

For Each link As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//a[@href]")
   Dim href = link.Attributes("href").Value
   If href.IndexOf("test", StringComparison.OrdinalIgnoreCase) >= 0 Then
       ListBox1.Items.Add(href)
       ' or
       ListBox1.Items.Add(link.InnerText)
   End If
Next 



Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi
Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi