Note that there are some explanatory texts on larger screens.

plurals
  1. POVB.net Getting the InnerText of href using HtmlAgilityPack
    text
    copied!<p>I have now updated my code (Thanks Tim for helping me learn) which is already working but it doesn't give me the right links i want. </p> <p>Here is my working code:</p> <pre><code> 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&amp;as_q=test&amp;as_epq=&amp;as_oq=&amp;as_eq=&amp;as_nlo=&amp;as_nhi=&amp;lr=&amp;cr=countryCA&amp;as_qdr=all&amp;as_sitesearch=&amp;as_occt=any&amp;safe=images&amp;tbs=ctr%3AcountryCA&amp;as_filetype=&amp;as_rights=#as_qdr=all&amp;cr=countryCA&amp;fp=1&amp;hl=en&amp;lr=&amp;q=test&amp;start=20&amp;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) &gt;= 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 </code></pre> <h2>I am currently new to this HtmlAgilityPack, I am still learning please bear with me.</h2> <p>My Main Goal:</p> <p>Sample link: <code>http://www.google.com.ph/search?hl=en&amp;as_q=test&amp;as_epq=&amp;as_oq=&amp;as_eq=&amp;as_nlo=&amp;as_nhi=&amp;lr=&amp;cr=countryCA&amp;as_qdr=all&amp;as_sitesearch=&amp;as_occt=any&amp;safe=images&amp;tbs=ctr%3AcountryCA&amp;as_filetype=&amp;as_rights=#as_qdr=all&amp;cr=countryCA&amp;fp=1&amp;hl=en&amp;lr=&amp;q=test&amp;start=20&amp;tbs=ctr:countryCA</code></p> <p>My expected link outputs which contains the word "test":</p> <pre><code>www.copetest.com/‎ www.testofhumanity.com/ www3.algonquincollege.com/testcentre/‎ www.lpitest.ca/‎ testtube.nfb.ca/‎ www.ieltscanada.ca/testdates.jsp‎ https://www.awinfosys.com/eassessment/fsa_fieldtest.htm‎ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload