使用VBA将表从Web导入Excel [英] Importing a table from the web to Excel using VBA

查看:133
本文介绍了使用VBA将表从Web导入Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写了一些基本的VBA连接到一个网站,输入一个用户名和密码到该网站,登录,然后复制一个表并将其粘贴到excel。现在我意识到我的表包含有链接的图形。而不是复制前面提到的表,我想单独遵循这些链接,并将这些链接复制到excel中。



但是,Web表中的这些图形不包含单独的直接超链接。我查看了网页源,它包含Javascript。 (见下文)

 < 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.gifalt =查看详细信息style =border:0; title =查看详细信息/>< / a> 

我可以在源代码中看到这些调用的函数,并记住我知道零Javascript,我的VBA代码如何执行/打开这些链接,然后移动到表中的第二行到下一个链接?基本上,如何填充onclick事件所需的变量,然后激活该超链接以打开新网页?



然后该表中的每个后续链接?

解决方案

您可以使用以下命令从VBA调用JavaScript函数:

 调用ie.document.parentWindow.execScript(functionName(),JavaScript)

在您的情况下,它将如下所示:

 调用ie.document.parentWindow.execScript (var a = function(){javascript:window.open('','ProcStatus','top = 50,left ='+(screen.width-750)+',width = 700,height = 500,resizable ,'status',''''''''''''''''''''' 319:j_id208'},'ProcStatus');} return false}; return(a()== false)?false:b();,JavaScript)
/ pre>

或者,您也可以指示您的宏使用DOM方法单击链接。您将不得不挖掘源代码,以确定元素位于何处,但一般来说,代码将沿着以下几行:

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

3是a元素的数组的索引。



您不需要知道很多JavaScript在webscraping中有效,但有助于了解一些基本的DOM方法。




  • 文档 .getElementsByTagName - 查找带有指定标记名的元素,例如 table a td tr div 等。

  • 文档 .getElementsByName - 查找具有给定名称的元素

  • 文档 .getElementById - 发现给定id的元素

  • 元素 .innerText - 返回元素的文本

  • 元素 .innerHTML - 返回元素的HTML

  • 元素 .click - - 点击元素

  • 元素 .getAttribute(attribute) - 返回给定属性的值,如 href style


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 webpage 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 webpage?

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

这篇关于使用VBA将表从Web导入Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆