简体   繁体   中英

Importing a table from the web to Excel using VBA

Wrote some basic VBA that connects to a website, inputs a username and password to that site, logs in, then copies a table and pastes it into excel. Now I realised that my table contains graphics with links. Instead of copying the table previously mentioned, I want to follow these links individually and copy the tables these link to, into Excel instead.

However, these graphics in the web table don't contain individual direct hyperlinks. I viewed the web page source, and it contains Javascript. (See below)

 <a href="#" onclick="var a=function(){javascript:window.open('','ProcStatus','top=50,left=' +     (screen.width - 750) + ',width=700,height=500,resizable,status,scrollbars');};var b=function()    {if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('MainPage'),        {'j_id202:319:j_id208':'j_id202:319:j_id208'},'ProcStatus');}return false};return (a()==false) ?     false : b();"><img src="image.gif" alt="View Details" style="border: 0;" title="View Details" /></a>

I can see the functions these call in the source code, and bearing in mind that I know zero about Javascript, how would my VBA code go about executing/opening these links, and then moving on to the second row in the table, to the next link? Basically, how do I populate the variables required by the onclick event, and then activate that hyperlink to open the new web page?

And then every subsequent link in that table?

You can call a JavaScript function from VBA by using the following:

Call ie.document.parentWindow.execScript("functionName()", "JavaScript")

In your case it would look like:

Call ie.document.parentWindow.execScript("var a=function(){javascript:window.open('','ProcStatus','top=50,left=' +     (screen.width - 750) + ',width=700,height=500,resizable,status,scrollbars');};var b=function()    {if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('MainPage'),        {'j_id202:319:j_id208':'j_id202:319:j_id208'},'ProcStatus');}return false};return (a()==false) ?     false : b();", "JavaScript")

Alternatively, you could also instruct your macro to click on the link using DOM methods. You will have to dig through the source code to find out exactly where the element is located, but in general you code will be along the following lines:

ie.document.getElementsByTagName("a")[3].click

Where 3 is the index of the array of "a" elements.

You don't need to know a whole lot of javascript to be effective at webscraping but it helps to know some basic DOM methods.

  • document .getElementsByTagName -- finds elements with a given tagname, such as table , a , td , tr , div , etc.
  • document .getElementsByName -- finds elements with a given name
  • document .getElementById -- finds elements with a given id
  • element .innerText -- returns the text of an element
  • element .innerHTML -- returns the HTML of an element
  • element .click -- clicks on an element
  • element .getAttribute("attribute") -- returns value of given attribute, like href or style

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM