VB - [FUNCTION] Get URLs from Google Search


SUBMITTED BY: Guest

DATE: March 31, 2013, 2:55 a.m.

FORMAT: Text only

SIZE: 1.7 kB

HITS: 1215

  1. 'LucidDevelopment (March 20, 2013 - 21:43)
  2. '!NO CREDITS REQUIRED! The code is mine and I give you all the permission to use without credits given.
  3. Function GetURLs(query As String) As List(Of String)
  4. Dim rtn As New List(Of String)
  5. Dim rgxSection As New System.Text.RegularExpressions.Regex("<h3 class="
  6. "r"
  7. ">(?<item>.*?)</h3>")
  8. Dim hrefRgx As New System.Text.RegularExpressions.Regex("(?:(?:http|https):\/\/)?([-a-zA-Z0-9.]{2,256}\.[a-z]{2,4})\b(?:\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?")
  9. Try
  10. Dim section As System.Text.RegularExpressions.MatchCollection = rgxSection.Matches((New System.Net.WebClient()).DownloadString(New Uri("http://www.google.com/search?q=" & query)))
  11. For Each m1 As System.Text.RegularExpressions.Match In section
  12. Dim url As System.Text.RegularExpressions.MatchCollection = hrefRgx.Matches(m1.Groups("item").Value)
  13. For Each m2 As System.Text.RegularExpressions.Match In url
  14. rtn.Add(m2.Value.Replace("%3F", "?").Replace("%3D", "=").Replace("&amp", ""))
  15. 'Simply cleaning up the URL and making it readable.
  16. Next
  17. Next
  18. Catch ex As Exception
  19. MessageBox.Show(ex.ToString & vbNewLine & vbNewLine & "GetURLs(...) - LucidDevelopment")
  20. End Try
  21. Return rtn
  22. End Function
  23. 'Usage
  24. Dim item As List(Of String) = GetURLs(TextBox1.Text)
  25. For Each s As String In item
  26. '"s" is the URL. Do something with it <3
  27. Next
  28. ///////////////////////////C#////////////////////////////////////

comments powered by Disqus