How to Create a Quick Bing Searcher in Visual Basic

Language

Introduction: Welcome to my tutorial on how to create a quick bing searcher in visual basic. It will search a given query and launch the first three links. Steps of Creation: Step 1: First we want to import some namespaces and create a function get a list of strings between two points within a given string...
  1. Imports System.Text.RegularExpressions
  2. Imports System.Net
  3. Imports System.IO
  1. Private Function GetBetweenAll(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String) As String()
  2. Dim Results, T As New List(Of String)
  3. T.AddRange(Regex.Split(Source, Str1))
  4. T.RemoveAt(0)
  5. For Each I As String In T
  6. Results.Add(Regex.Split(I, Str2)(0))
  7. Next
  8. Return Results.ToArray
  9. End Function
Step 2: Now we want to initialize the search query. We must replace any spaces with a plus symbol in order for it to work properly in the url.
  1. Dim q As String = TextBox1.Text
  2. If (q.Contains(" ")) Then q.Replace(" ", "+")
Step 3: Now we want to go to the search query page and retrieve the source code. We then want to extract all the links within the page.
  1. Dim link As String = "http://www.bing.com/search?q=" & q & "&go=&qs=bs&form=QBLH&filt=all"
  2. Dim r As HttpWebRequest = HttpWebRequest.Create(link)
  3. Dim re As HttpWebResponse = r.GetResponse()
  4. Dim src As String = New StreamReader(re.GetResponseStream()).ReadToEnd()
  5. Dim links As String() = GetBetweenAll(src, "<h3><a href=""", """")
Step 4: Now we want to run through all the links and add the first three that are stand alone links (http://) to a new list. We then want to run them all.
  1. Dim urls As New List(Of String)
  2. Dim i As Integer = 0
  3. Do Until urls.Count() >= 3
  4. If (links(i).StartsWith("http://")) Then urls.Add(links(i))
  5. i += 1
  6. Loop
  7. For Each u As String In urls
  8. Diagnostics.Process.Start(u)
  9. Next
Project Complete! Below is the full source code and download to the project files.
  1. Imports System.Text.RegularExpressions
  2. Imports System.Net
  3. Imports System.IO
  4.  
  5. Public Class Form1
  6. Private Function GetBetweenAll(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String) As String()
  7. Dim Results, T As New List(Of String)
  8. T.AddRange(Regex.Split(Source, Str1))
  9. T.RemoveAt(0)
  10. For Each I As String In T
  11. Results.Add(Regex.Split(I, Str2)(0))
  12. Next
  13. Return Results.ToArray
  14. End Function
  15. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  16. Dim q As String = TextBox1.Text
  17. If (q.Contains(" ")) Then q.Replace(" ", "+")
  18. Dim link As String = "http://www.bing.com/search?q=" & q & "&go=&qs=bs&form=QBLH&filt=all"
  19. Dim r As HttpWebRequest = HttpWebRequest.Create(link)
  20. Dim re As HttpWebResponse = r.GetResponse()
  21. Dim src As String = New StreamReader(re.GetResponseStream()).ReadToEnd()
  22. Dim links As String() = GetBetweenAll(src, "<h3><a href=""", """")
  23. Dim urls As New List(Of String)
  24. Dim i As Integer = 0
  25. Do Until urls.Count() >= 3
  26. If (links(i).StartsWith("http://")) Then urls.Add(links(i))
  27. i += 1
  28. Loop
  29. For Each u As String In urls
  30. Diagnostics.Process.Start(u)
  31. Next
  32. End Sub
  33. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment