Guest User

Untitled

a guest
Jan 4th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. Sub SearchBot()
  2.  
  3. 'dimension (declare or set aside memory for) our variables
  4. Dim objIE As InternetExplorer 'special object variable representing the IE browser
  5. Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
  6. Dim y As Integer 'integer variable we'll use as a counter
  7. Dim result As String 'string variable that will hold our result link
  8.  
  9. 'initiating a new instance of Internet Explorer and asigning it to objIE
  10. Set objIE = New InternetExplorer
  11.  
  12. 'make IE browser visible (False would allow IE to run in the background)
  13. objIE.Visible = True
  14.  
  15. 'navigate IE to this web page (a pretty neat search engine really)
  16. objIE.navigate "https://duckduckgo.com"
  17.  
  18. 'wait here a few seconds while the browser is busy
  19. Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
  20.  
  21. 'in the search box put cell "A2" value, the word "in" and cell "C1" value
  22. objIE.document.getElementById("search_form_input_homepage").Value = _
  23. Sheets("Sheet1").Range("A2").Value & " in " & Sheets("Sheet1").Range("C1").Value
  24.  
  25. 'click the 'go' button
  26. objIE.document.getElementById("search_button_homepage").Click
  27.  
  28. 'wait again for the browser
  29. Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
  30.  
  31. 'the first search result will go in row 2
  32. y = 2
  33.  
  34. 'for each <a> element in the collection of objects with class of 'result__a'...
  35. For Each aEle In objIE.document.getElementsByClassName("result__a")
  36.  
  37. '...get the href link and print it to the sheet in col C, row y
  38. result = aEle
  39. Sheets("Sheet1").Range("C" & y).Value = result
  40.  
  41. '...get the text within the element and print it to the sheet in col D
  42. Sheets("Sheet1").Range("D" & y).Value = aEle.innerText
  43. Debug.Print aEle.innerText
  44.  
  45. 'is it a yellowpages link?
  46. If InStr(result, "yellowpages.com") > 0 Or InStr(result, "yp.com") > 0 Then
  47. 'make the result red
  48. Sheets("Sheet1").Range("C" & y).Interior.ColorIndex = 3
  49. 'place a 1 to the left
  50. Sheets("Sheet1").Range("B" & y).Value = 1
  51. End If
  52.  
  53. 'increment our row counter, so the next result goes below
  54. y = y + 1
  55.  
  56. 'repeat times the # of ele's we have in the collection
  57. Next
  58.  
  59. 'add up the yellowpages listings
  60. Sheets("Sheet1").Range("B1").Value = _
  61. Application.WorksheetFunction.Sum(Sheets("Sheet1").Range("B2:B100"))
  62.  
  63. 'close the browser
  64. objIE.Quit
  65.  
  66. 'exit our SearchBot subroutine
  67. End Sub
Add Comment
Please, Sign In to add comment