1

How does one get the description of the first link to appear on google search results?

TEST CODE UPDATE 1/24/2017

Been getting the ' object doesnt support method/property' message

Dim URl As String, lastRow As Long
Dim xmlHttp As Object, html As Object, objResultDiv As Object, objH3 As Object, link As Object



lastRow = Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastRow

    URl = "https://www.google.co.in/search?q=" & Cells(i, 1)

    Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
    xmlHttp.Open "GET", URl, False
    xmlHttp.setRequestHeader "Content-Type", "text/xml"
    xmlHttp.send

    Set html = CreateObject("htmlfile")
    html.body.innerHTML = xmlHttp.ResponseText
    Set objResultDiv = html.getelementbyid("rso")
    Set objH3 = objResultDiv.getelementsbytagname("H3")(0)
    Set link = objH3.getelementsbytagname("a")(0)
    Set objResultDiv = html.GetElementsByClassName("f slp")(0)

    str_text = Replace(link.innerHTML, "<EM>", "")
    str_text = Replace(str_text, "</EM>", "")

    Cells(i, 2) = str_text
    Cells(i, 3) = link.href
    Cells(i, 4) = objResultDiv.innerText
  • Can you please explain, what do you mean by "the text below the link"? If you search for [Stack overflow on google](https://www.google.sk/?gfe_rd=cr&ei=fe-EWLGgFLGE8QewjrLQAQ&gws_rd=ssl#q=stack+overflow), what is the text you are after? The "Stack Overflow" or the "Stack Overflow is the largest online community for programmers to learn, share their knowledge, and advance their careers." – kolcinx Jan 22 '17 at 17:46
  • Please provide at least one or two concrete examples of search phrase and desired outputs. – kolcinx Jan 23 '17 at 13:02
  • If i search "Accounting for Investments, Equities, Futures and Options" I am looking to fetch the "R. Venkata Subramani - 2011 - ‎Business & Economics" which is under the green url – Abbie Latonio Jan 23 '17 at 15:06

1 Answers1

1

Span class="st"

I believe you are looking for <span class="st"> inside the <div class="rc">

Edit #1

If we search for Accounting for Investments, Equities, Futures and Options, and we want the text R. Venkata Subramani - 2011 - ‎Business & Economics, below the green url, like in the picture:

enter image description here

Then what we need is indeed the <div class="f slp">, as you OP pointed out.
enter image description here

So to get the text from it we can use code:

set objResultDiv = html.GetElementsByClassName("f slp")(0) 'note the plural, element*s*
cells(i, 4) = objResultDiv.innerText

Object doesn't support this property or method response

HTMLDocument has one additional method to get a collection of elements.

set objResultDiv = .querySelectorAll(".f.slp")
cells(i, 4) = objResultDiv(index).innerText` 'replace index with number
kolcinx
  • 2,183
  • 1
  • 15
  • 38
  • i am vaguely familiar with vba..is it coded this way? (refer to updated post) @branislav-kollár – Abbie Latonio Jan 22 '17 at 17:01
  • actually looking for the text inside
    – Abbie Latonio Jan 22 '17 at 17:11
  • @AbbieLatonio See Edit #1 in my answer, hope it helps. – kolcinx Jan 23 '17 at 16:50
  • thank you sir, i have tried including the code but 'object doesnt support this property or method' would always flash. am i missing anything? – Abbie Latonio Jan 24 '17 at 09:14
  • @AbbieLatonio, I have updated my answer. Based on tips given in http://stackoverflow.com/questions/9568969/getelementsbyclassname-ie8-object-doesnt-support-this-property-or-method – kolcinx Jan 24 '17 at 11:07
  • i tried including the code but it flashes 'invalid reference' while highlighitng .queryselectorAll, so i tried to include 'html' before .queryselectorAll, and 'document' in another instance... (html.queryselectorall & document.queryselectorall) having both to fail as well with 'object doesnt support this method' msg – Abbie Latonio Jan 24 '17 at 11:42
  • @AbbieLatonio, you are right. The html object was missing from my code, I had it inside the `With` block. For the problem at hand, I ran out of ideas. – kolcinx Jan 24 '17 at 12:35