Sub Two()
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://example.com/market/listings/578080/Sneakers%20(WHITE)"
Do: DoEvents: Loop Until IE.ReadyState = 4
Srd27 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText
ActiveSheet.Range("D27").Value = Srd27
IE.Navigate "http://example.com/market/listings/578080/Floral%20Shirt%20(Black)"
Do: DoEvents: Loop Until IE.ReadyState = 4
Srd28 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText
ActiveSheet.Range("D28").Value = Srd28
IE.Navigate "http://example.com/market/listings/578080/Tracksuit%20Top%20(Yellow)"
Do: DoEvents: Loop Until IE.ReadyState = 4
Srd29 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText
ActiveSheet.Range("D29").Value = Srd29
IE.Navigate "http://example.com/market/listings/578080/School%20Jacket"
Do: DoEvents: Loop Until IE.ReadyState = 4
Srd30 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText
ActiveSheet.Range("D30").Value = Srd30
IE.Navigate "http://example.com/market/listings/578080/Leather%20Bootcut%20Pants"
Do: DoEvents: Loop Until IE.ReadyState = 4
Srd31 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText
ActiveSheet.Range("D31").Value = Srd31
IE.Quit
End Sub
如果我在Visual Basic应用程序中使用 F8 键,它有时会工作。但是当我在Excel中使用宏时,它的说法是
'对象变量或未设置块变量 - 错误'91''
答案 0 :(得分:0)
我已对其进行了测试,从我可以看到的地址“http://steamcommunity.com/market/listings/578080/Sneakers%20(WHITE)”为您提供了一个页面,说明无法找到该产品。 方法getElementsByClassName生成错误,因为您要查找的元素在加载的页面上不可用。
尝试这样的事情:
Sub Two()Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://steamcommunity.com/market/listings/578080/Sneakers%20(WHITE)" Do: DoEvents: Loop Until IE.ReadyState = 4 on error resume next Srd27 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0).innerText on error goto 0 If Srd27 <> "" then ActiveSheet.Range("D27").Value = Srd27 Else ActiveSheet.Range("D27").Value = "Product not found" end if
'repeat for the rest of the code
ListableBeanFactory
当然,我总是鼓励使用Option Explicit并声明所有变量。
答案 1 :(得分:0)
在尝试访问其属性之前,您需要设置对象并验证其状态。
<强> getElementsByClassName方法()强>
返回具有相同类属性值的对象集合。
Sub Two()
Dim IE As Object, Srd27 As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://steamcommunity.com/market/listings/578080/Sneakers%20(WHITE)"
Do: DoEvents: Loop Until IE.ReadyState = 4
Set Srd27 = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")(0)
If Not Srd27 Is Nothing Then
ActiveSheet.Range("D27").Value = Srd27.innerText
End If
End Sub
要获得多个结果,您必须遍历元素集合并获取每个元素的innerText。
Dim elements As Object, element as Object
Set elements = IE.Document.getElementsByClassName("market_commodity_orders_header_promote")
If Not elements Is Nothing Then
For Each element in elements
Debug.Print element.innerText
Next
End if