我是VBA的新手,我想从一个网站将以太坊价格导入Excel,并使用VBA。
Sub go()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.navigate "https://www.coingecko.com/de/munze/ethereum.html"
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
Set getPrice = appIE.document.getElementsByTagName("span.currency-exchangable")
Dim myValue As String: myValue = getPrice.innerText
appIE.Quit
Set appIE = Nothing
Range("B1").Value = myValue
End Sub
我也在参考文献下激活了Microsoft Internet Control
,Microsoft Object HTML Library
和Microsoft ActiveX Data Objects 6.1 library
。
错误消息是
运行时错误438 - 对象不支持此属性或方法。
我只想输入以太坊的价格= $ 1,371.43
答案 0 :(得分:2)
以下是如何获得价格:
Sub fetch_price()
Dim IE As New InternetExplorer, html As HTMLDocument
Dim getprice As Object
With IE
.Visible = False
.navigate "https://www.coingecko.com/de/munze/ethereum.html"
While .readyState < 4: DoEvents: Wend
Set html = .document
End With
Set getprice = html.getElementsByClassName("currency-exchangable")(1)
[A1] = getprice.innerText
IE.Quit
End Sub
参考添加到库:
1. Microsoft HTML Object Library
2. Microsoft Internet Controls
使用xmlhttp
请求。它比IE快,但数字格式出了问题。我使用queryselector
来避免硬编码索引。
Sub fetch_price()
Dim HTTP As New XMLHTTP60, html As New HTMLDocument
Dim getprice As Object
With HTTP
.Open "GET", "https://www.coingecko.com/de/munze/ethereum.html", False
.send
html.body.innerHTML = .responseText
End With
Set getprice = html.querySelector(".coin-value span[class='currency-exchangable']")
[A1] = getprice.innerText
End Sub
参考添加到库:
1. Microsoft HTML Object Library
2. Microsoft XML, v6.0 'or the version you have
答案 1 :(得分:2)
您可以使用以下内容。
注意:
我在最后提供的链接优先于Internet Explorer上的Sub AddIcon
来检索HTML。当然,我的经历更快。
另外,请勿将保留字XMLHTTP60
用于过程名称。它会引发错误。
您需要按类名访问。
Go
参考:Excel VBA Introduction Part 47 - Browsing to Websites and Scraping a Web Page
通过在此处添加对Microsoft XML库的引用,可以更快速地检索信息:
Sub Testing()
'ms html library reference
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.navigate "https://www.coingecko.com/de/munze/ethereum.html"
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
Dim HTMLDoc As MSHTML.HTMLDocument
Set HTMLDoc = appIE.Document
Dim HTMLItemCol As MSHTML.IHTMLElementCollection
Set HTMLItemCol = HTMLDoc.getElementsByClassName("currency-exchangable")
Dim myValue As String
myValue = HTMLItemCol.Item(1).innerText
appIE.Quit
Set appIE = Nothing
ActiveSheet.Range("B1").Value = myValue
End Sub