从网站提取数据 excel 错误
Extracting data from website to excel error
我在设置从网站到 Excel 的数据提取时遇到困难。
我想将产品的确切价格提取到 excel。
到目前为止我有这个代码:
Sub GetData()
Dim objIE As InternetExplorer 'Microsoft Internet Controls library added
Dim itemEle As Object
Dim data As String
Dim y As Integer
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.nay.sk/samsung-ue55nu7172"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
y = 1
For Each itemEle In objIE.document.getElementsByClassName("price")
data = itemEle.getElementsByClassName("price")(0).innerText
y = y + 1
Next
data = Range("A1").Value
End Sub
你有什么建议?
试试这个:
Sub GetData()
Dim objIE As New InternetExplorer 'Microsoft Internet Controls library added
Dim itemEle As Object
Dim data As String
Dim y As Integer
objIE.Visible = True
objIE.navigate "https://www.nay.sk/samsung-ue55nu7172"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
y = 1
For Each itemEle In objIE.document.getElementsByClassName("price")
Cells(y, 1) = itemEle.outertext
y = y + 1
Next
End Sub
这是你得到的:
获取 itemEle
的正确属性:
- 在下面打印屏幕的行上放置一个停止标志
- select
itemEle
用鼠标
- 按Shift+F9
每个价格都要吗?
您可以这样列出前两个示例:
Option Explicit
Public Sub GetInfo()
Dim sResponse As String, i As Long, html As New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.nay.sk/samsung-ue55nu7172", False
.send
sResponse = StrConv(.responseBody, vbUnicode)
End With
sResponse = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))
Dim titles As Object, prices As Object
With html
.body.innerHTML = sResponse
Set titles = .querySelectorAll(".title")
Set prices = .querySelectorAll(".price")
End With
For i = 0 To 1
Debug.Print titles(i).innerText & prices(i).innerText
Next i
End Sub
那个循环 returns 你这些:
您实际上已经将页面上所有带有 price
class 的元素存储在对象 prices
.
中
您可以通过循环 object/nodeList 的长度来查看所有价格:
For i = 0 To prices.Length - 1
Debug.Print Prices.item(i).innerText
Next i
同样,您可以循环 titles
的 .Length
,但请注意它与 prices
的长度不同。页面上有更多价格(或者更确切地说,具有 price
class 的元素与具有 title
class.
的元素相比
参考(VBE>工具>参考):
- HTML 对象库
我在设置从网站到 Excel 的数据提取时遇到困难。 我想将产品的确切价格提取到 excel。 到目前为止我有这个代码:
Sub GetData()
Dim objIE As InternetExplorer 'Microsoft Internet Controls library added
Dim itemEle As Object
Dim data As String
Dim y As Integer
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.nay.sk/samsung-ue55nu7172"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
y = 1
For Each itemEle In objIE.document.getElementsByClassName("price")
data = itemEle.getElementsByClassName("price")(0).innerText
y = y + 1
Next
data = Range("A1").Value
End Sub
你有什么建议?
试试这个:
Sub GetData()
Dim objIE As New InternetExplorer 'Microsoft Internet Controls library added
Dim itemEle As Object
Dim data As String
Dim y As Integer
objIE.Visible = True
objIE.navigate "https://www.nay.sk/samsung-ue55nu7172"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
y = 1
For Each itemEle In objIE.document.getElementsByClassName("price")
Cells(y, 1) = itemEle.outertext
y = y + 1
Next
End Sub
这是你得到的:
获取 itemEle
的正确属性:
- 在下面打印屏幕的行上放置一个停止标志
- select
itemEle
用鼠标 - 按Shift+F9
每个价格都要吗?
您可以这样列出前两个示例:
Option Explicit
Public Sub GetInfo()
Dim sResponse As String, i As Long, html As New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.nay.sk/samsung-ue55nu7172", False
.send
sResponse = StrConv(.responseBody, vbUnicode)
End With
sResponse = Mid$(sResponse, InStr(1, sResponse, "<!DOCTYPE "))
Dim titles As Object, prices As Object
With html
.body.innerHTML = sResponse
Set titles = .querySelectorAll(".title")
Set prices = .querySelectorAll(".price")
End With
For i = 0 To 1
Debug.Print titles(i).innerText & prices(i).innerText
Next i
End Sub
那个循环 returns 你这些:
您实际上已经将页面上所有带有 price
class 的元素存储在对象 prices
.
您可以通过循环 object/nodeList 的长度来查看所有价格:
For i = 0 To prices.Length - 1
Debug.Print Prices.item(i).innerText
Next i
同样,您可以循环 titles
的 .Length
,但请注意它与 prices
的长度不同。页面上有更多价格(或者更确切地说,具有 price
class 的元素与具有 title
class.
参考(VBE>工具>参考):
- HTML 对象库