【发布时间】:2014-05-11 14:53:53
【问题描述】:
我这几天一直在寻找信息,但是我找到的所有示例都只有一小部分代码,我需要它=)
我想要做的是从主页中提取一个值并将其放入 Excel 中的单元格中 (然后从同一站点上的另一个页面获取另一个值并放入下一个单元格等)
该页面是瑞典证券交易所页面,我用作测试页面的页面是“投资者 B”(https://www.avanza.se/aktier/om-aktien.html/5247/investor-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 脚本,在阅读了一些论坛帖子(主要是这里)之后,我自己尝试了一些示例,但无法工作。 由于我对 VBA 很基础,我可能结构错误,所以请对我保持基本和耐心,这是我的测试,但我得到“运行时错误 429” ActiveX 组件无法创建对象
我可能完全走错了路
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 17:05 测试当前代码
按钮命令下
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
在模块1中
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
【问题讨论】:
-
这是我正在使用的测试文档的download link。它有两张纸,每张纸使用不同的方法。
-
如果我将它添加到 Private Sub CommandButton1_Click() 行上方,我将获得与以前相同的行为如果我将其放在该行下方,我将收到一条错误消息“预期结束子”,猜测该功能不能在 command_button sub 我不知道这是否无关紧要,但是做了一些高级谷歌搜索,你们怎么看?最好的问候social.msdn.microsoft.com/Forums/office/en-US/…
-
在这两种情况下,我都收到了与上述完全相同的错误消息 非常感谢您在此问题上花费的时间。这似乎是一个 excel 2013 的问题,或者是我的计算机设置问题。知道如何继续寻找问题的答案吗?
-
是的,我的意思是你的文件(两种情况)所以它必须是计算机/设置所以我想知道如何让你的代码在我的机器上工作,得到 IE 9.0.8112.16421 更新版本 9.0。 26 有什么想法吗?
-
设法在excel 2010中测试它,同样的错误,因为我现在在海外,我没有可能在另一台机器上测试它,我这个周末试试,任何想法为什么会出现此错误消息?我运行 Windows 7 Enterprise 64 位
标签: excel web-scraping html vba