Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Code to pull text from google search panel

0

I was following the following video to try to create a section for a code to pull basic information on companies from the google knowledge panel to populate into an excel spreadsheet. In my example, I am trying to capture the address text for the company: 10 Huntington Ave, Boston, MA 02116.

Video: https://www.youtube.com/watch?v=IOzHacoP-u4

When I run the code, I get Run-time error '91'

Sub Get_Web_Data()

Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim address As Variant

website = "https://www.google.com/search?q=Weston Copley 10 Huntington Ave, Boston, MA"

Set request = CreateObject("MSXML2.XMLHTTP")

request.Open "GET", website, False

request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"

request.send

response = StrConv(request.responseBody, vbUnicode)

html.body.innerHTML = response

address = html.getElementsByClassName("LrzXr")(0).innerText

MsgBox address

End Sub


The issue is associated with this line:
address = html.getElementsByClassName("LrzXr")(0).innerText


I have activated all the references specified in the video. Thanks!
Answer
Discuss

Answers

0
Selected Answer

Hi Ocelot and welcome to the Forum

The problem seems to be that your address:

website = "https://www.google.com/search?q=Weston Copley 10 Huntington Ave, Boston, MA"

just delivers the results of a Google search (on that street address) which does NOT contain an element with ClassName "LrzXr".

Either change your website for one that contains that element or change the failed line to refer to one that exists e.g. (in bold):

address = html.getElementsByClassName("CvDJxb")(0).innerTex
Discuss

Discussion

Hi John, thank you for replying. When I use the link, I do get a search result that contains the element with ClassName "LrzXr". Can you advise on why we're getting divergent search results? 

Ocelot (rep: 2) Oct 19, '22 at 11:44 am
I switched over to the yahoo search engine and it works. For some reason, google doesn't load the knowledge panel on the first go. Thanks for the help!
Ocelot (rep: 2) Oct 19, '22 at 12:22 pm
Glad you got it sorted. I was using Google Chrome incidentally. 
John_Ru (rep: 6142) Oct 19, '22 at 1:42 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login