使用 Excel VBA 从 HTML 获取跨度 class
get span class from HTML using Excel VBA
我正在尝试使用 VBA 从 AliExpress 产品页面获取价格。
但是我收到运行时错误“91”:未设置对象变量或 With 块变量..
这是我的代码:
Sub Get_Web_Data()
Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim price As Variant
' Website to go to.
website = "https://de.aliexpress.com/item/1005002587077651.html"
' Create the object that will make the webpage request.
Set request = CreateObject("MSXML2.XMLHTTP")
' Where to go and how to go there - probably don't need to change this.
request.Open "GET", website, False
' Get fresh data.
request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
' Send the request for the webpage.
request.send
' Get the webpage response data into a variable.
response = StrConv(request.responseBody, vbUnicode)
' Put the webpage into an html object to make data references easier.
html.body.innerHTML = response
' Get the price from the specified element on the page.
price = html.getElementsByClassName("product-price-value").Item(0).innerText
' Output the price into a message box.
MsgBox price
End Sub
其他页面的代码有问题,比如 Yahoo Finance,我只遇到 AliExpress 的问题。
谢谢。
您可以使用正则表达式
Sub Get_Web_Data()
Dim reg As Object
Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim price As Variant
Set reg = CreateObject("vbscript.regexp")
reg.Pattern = "totalValue: ""US $(.+)"""
website = "https://de.aliexpress.com/item/1005002587077651.html"
Set request = CreateObject("WinHttp.WinHttpRequest.5.1")
request.Open "GET", website, False
request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
request.send
Set matches = reg.Execute(request.responseText)
MsgBox "US $" & matches.Item(0).submatches(0)
End Sub
我正在尝试使用 VBA 从 AliExpress 产品页面获取价格。
但是我收到运行时错误“91”:未设置对象变量或 With 块变量.. 这是我的代码:
Sub Get_Web_Data()
Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim price As Variant
' Website to go to.
website = "https://de.aliexpress.com/item/1005002587077651.html"
' Create the object that will make the webpage request.
Set request = CreateObject("MSXML2.XMLHTTP")
' Where to go and how to go there - probably don't need to change this.
request.Open "GET", website, False
' Get fresh data.
request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
' Send the request for the webpage.
request.send
' Get the webpage response data into a variable.
response = StrConv(request.responseBody, vbUnicode)
' Put the webpage into an html object to make data references easier.
html.body.innerHTML = response
' Get the price from the specified element on the page.
price = html.getElementsByClassName("product-price-value").Item(0).innerText
' Output the price into a message box.
MsgBox price
End Sub
其他页面的代码有问题,比如 Yahoo Finance,我只遇到 AliExpress 的问题。
谢谢。
您可以使用正则表达式
Sub Get_Web_Data()
Dim reg As Object
Dim request As Object
Dim response As String
Dim html As New HTMLDocument
Dim website As String
Dim price As Variant
Set reg = CreateObject("vbscript.regexp")
reg.Pattern = "totalValue: ""US $(.+)"""
website = "https://de.aliexpress.com/item/1005002587077651.html"
Set request = CreateObject("WinHttp.WinHttpRequest.5.1")
request.Open "GET", website, False
request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
request.send
Set matches = reg.Execute(request.responseText)
MsgBox "US $" & matches.Item(0).submatches(0)
End Sub