尝试在Excel中使用VBA从网页中提取一个值

我一直试图find信息几天,但所有我发现的例子只是一小段代码,我需要它=)

我想要做的是从主页中提取一个值,并将其放入Excel中的一个单元格(然后从另一个页面上的另一个值在同一个网站上,并放入下一个单元格等等)

该页面是一个瑞典股票交易页面,我用作testing页面的页面是“投资者B”的股票( https://www.avanza.se/aktier/om-aktien.html/5247 /投资者-b )

而我感兴趣的价值就是所谓的“Senaste”(这是围绕它的页面信息)

<li> <span class="XSText">Senast<br/></span> <span class="lastPrice SText bold"><span class="pushBox roundCorners3" title="Senast uppdaterad: 17:29:59">248,60</span></span> </li> 

这是我以后的价值248,60!

我有一些编程经验,但不是VBA脚本,在阅读了一些论坛post(大部分是这里)之后,我一直在尝试一些例子,但是没有得到任何工作。 由于我对VBA基本很熟,所以我可能会弄错结构,所以请对我基本耐心,这是我的testing,但是我得到了“运行时错误429”的ActiveX组件不能创build对象

我可能完全走错了路

 Private Sub CommandButton1_Click() Dim ie As Variant Set ie = CreateObject("InternetExplorer") ie.navigate "https://www.avanza.se/aktier/om-aktien.html/5247/investor-b" ie.Visible = True Do DoEvents Loop Until ie.readyState = READYSTATE_COMPLETE Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading Dim doc As Variant 'variable for document or data which need to be extracted out of webpage Set doc = CreateObject("HTMLDocument") Set doc = ie.document Dim dd As Variant dd = doc.getElementsByClassName("lastPrice SText bold")(0).innerText MsgBox dd End Sub 

编辑:2014-05-12当前代码beeingtesting17:05

在button命令下

 Private Sub CommandButton1_Click() Dim IE As Object ' Create InternetExplorer Object Set IE = CreateObject("InternetExplorer.Application") ' You can uncoment Next line To see form results IE.Visible = False ' Send the form data To URL As POST binary request IE.Navigate "https://www.avanza.se/aktier/om-aktien.html/5247/investor-b" ' Statusbar Application.StatusBar = "Loading, Please wait..." ' Wait while IE loading... 'Do While IE.Busy ' Application.Wait DateAdd("s", 1, Now) 'Loop 'this should go from ready-busy-ready IEWait IE Application.StatusBar = "Searching for value. Please wait..." ' Dim Document As HTMLDocument ' Set Document = IE.Document Dim dd As Variant dd = IE.Document.getElementsByClassName("lastPrice SText bold")(0).innerText MsgBox dd ' Show IE IE.Visible = True ' Clean up Set IE = Nothing Set objElement = Nothing Set objCollection = Nothing Application.StatusBar = "" End Sub 

并在module1中

 Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Public Function IEWait(p_ieExp As InternetExplorer) 'this should go from ready-busy-ready Dim initialReadyState As Integer initialReadyState = p_ieExp.ReadyState 'wait 250 ms until it's done Do While p_ieExp.Busy Or p_ieExp.ReadyState <> READYSTATE_COMPLETE Sleep 250 Loop End Function 

如前所述,我不知道这个最新加载项是否能够正确地结构化,不会因为这种编码而过期。

最好的祝福

停止编辑2014-05-12 17:08

你很近,但有一些小错误。

这是我将如何设置( testing ):

 Private Sub CommandButton1_Click() Dim IE As Object ' Create InternetExplorer Object Set IE = CreateObject("InternetExplorer.Application") ' You can uncoment Next line To see form results IE.Visible = False ' URL to get data from IE.Navigate "https://www.avanza.se/aktier/om-aktien.html/5247/investor-b" ' Statusbar Application.StatusBar = "Loading, Please wait..." ' Wait while IE loading... Do While IE.Busy Application.Wait DateAdd("s", 1, Now) Loop Application.StatusBar = "Searching for value. Please wait..." Dim dd As String dd = IE.Document.getElementsByClassName("lastPrice SText bold")(0).innerText MsgBox dd ' Show IE IE.Visible = True ' Clean up Set IE = Nothing Application.StatusBar = "" End Sub 

结果:

在这里输入图像说明


在Excel 2010中testing以下参考:

在这里输入图像说明


编辑 – 选项B

为了摆脱一个可能的“运行时错误”91“”尝试改变这样几行:

 Dim dd As Variant Set dd = IE.Document.getElementsByClassName("lastPrice SText bold") MsgBox dd(0).textContent 

编辑 – 选项C

另一种获得元素的方法是:

 Dim tag Dim tags As Object Set tags = IE.Document.getElementsByTagName("*") For Each tag In tags If tag.className = "lastPrice SText bold" Then MsgBox tag.innerText Exit For End If Next tag 

(这三种方法都在Excel 2010和IE10上testing过)

我只是想添加我目前正在运行的代码,如果人们遇到同样的问题,那么这个代码就可以很好地工作了。 这是为了获得两个值到专门的单元格。

 Private Sub CommandButton10_Click() Dim IE As Object Dim dd As Variant ' Create InternetExplorer Object Set IE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}") IE.Visible = False ' Send the form data To URL As POST binary request IE.Navigate "https://www.avanza.se/aktier/om-aktien.html/52476/alk-abell-b" Application.StatusBar = "Loading, Please wait..." IEWait IE Application.StatusBar = "Searching for value. Please wait..." dd = IE.Document.getElementsByClassName("lastPrice SText bold")(0).innerText Range("Y2").Value = dd IE.Navigate "https://www.avanza.se/aktier/om-aktien.html/52380/alm--brand" Application.StatusBar = "Loading, Please wait..." IEWait IE Application.StatusBar = "Searching for value. Please wait..." dd = IE.Document.getElementsByClassName("lastPrice SText bold")(0).innerText Range("Y3").Value = dd ' Clean up Set IE = Nothing Set objElement = Nothing Set objCollection = Nothing Application.StatusBar = "" End Sub