-1

I am looking to adapt the code presented by Santosh so that I can return the top ten links, not just the top one result, of a particular Google search. I need to upload > 1000 search queries and map the results against expected results but I care about more than just the #1 result, I'm looking to see if it returns within the top ten. I looked into the html and VBA and I can't figure it out.

Using VBA in Excel to Google Search in IE and return the hyperlink of the first result

Community
  • 1
  • 1

1 Answers1

0

Without testing, it looks like this line here is pulling one element in a collection:

    Set link = objH3.getelementsbytagname("a")(0)

So, you're setting the "Link" object to the first object (object 0) in the objH3 collection that has the "a" tag.

What you want to do is loop through that collection. Ex:

    Set links = objH3.getelementsbytagname("a")
    For i = 0 To 9
      set link = links(i)
      'do stuff
    next

Edit:

The loop should be on a higher level of objects:

    For i = 0 To 5 
        Set objH3 = objResultDiv.getelementsbytagname("H3")(i) 
        Set link = objH3.getelementsbytagname("a")(0) 
        ' Do Stuff 
    Next I

Thank you, Richard, for the update.

JMcD
  • 100
  • 7
  • Hi, if the answer isn't helpful enough, let me know and I'll update. If it is, please mark it as the accepted answer. – JMcD May 22 '15 at 15:57
  • Thanks JMcD! This looks like the perfect solution but I'm having some trouble getting it to run. It still goes through the first iteration just fine but when i goes to the second value (1) it errors out - RTE 91; variable not set. I'm guessing that after 0, values 1-9 are not valid (I did try forcing it to 2 with no luck). Here is the html for search result 1 & 2 on the webpage but I can't find an identifier to cycle through – Richard Karl Pfister May 23 '15 at 00:18
  • The Way I Text Ruined My Dating Life. See If It's Ruining ... 10 Texting and Online Dating Tips for Tech-Savvy ... - Shape – Richard Karl Pfister May 23 '15 at 00:18
  • Got it to work! I had to cycle through at a higher level: For i = 0 To 5 Set objH3 = objResultDiv.getelementsbytagname("H3")(i) Set link = objH3.getelementsbytagname("a")(0) ' Do Stuff Next i – Richard Karl Pfister May 23 '15 at 01:26
  • Glad you got it to work! Sorry I didn't see your comments early enough to help out more. – JMcD May 24 '15 at 19:24
  • Hi, if my answer helped, please mark it as accepted. That will give you 5 rep. It's hard getting rep around here when you're first starting off. – JMcD May 25 '15 at 15:05