Select HTML 项目使用 VBA
Select HTML item using VBA
我正在尝试从此 website 中抓取数据,我需要 select 图片中的值(在本例中为“Москва, Новорязанское ш”):
我无法确定要使用的正确 "class",也不确定可以利用哪些函数来定位我要保留的特定值。
任何指导将不胜感激。
我拥有的代码:
Public Sub Selected()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.castorama.ru/building-materials/building-dry-materials-and-primers"
While .Busy Or .readyState <> 4: DoEvents: Wend
.document.getElementsByClassName("store-switcher__current-store-i").querySelector("[shop='Воронеж']").Click
Stop
End With
End Sub
如果您检查页面的 HTML,您会看到每个商店都有自己的 data-id
属性值。你可以select通过这个属性的值点击合适的店名节点:
Option Explicit
Public Sub MakeShopSelection()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate2 "https://www.castorama.ru/building-materials/building-dry-materials-and-primers"
While .Busy Or .readyState <> 4: DoEvents: Wend
.document.querySelector("[data-id='36']").Click
While .Busy Or .readyState <> 4: DoEvents: Wend
Stop
.Quit
End With
End Sub
下面为您列出ids:
Option Explicit
Public Sub ListIds()
Dim ie As SHDocVw.InternetExplorer, idDict As Object, shopNames As Object
Set ie = New SHDocVw.InternetExplorer
Set idDict = CreateObject("Scripting.Dictionary")
With ie
.Visible = True
.navigate2 "https://www.castorama.ru/building-materials/building-dry-materials-and-primers"
While .Busy Or .readyState <> 4: DoEvents: Wend
Dim i As Long
With .document
Set shopNames = .querySelectorAll(".shop__name")
For i = 0 To shopNames.Length - 1
idDict(Trim$(shopNames.Item(i).innertext)) = shopNames.Item(i).getAttribute("data-id")
Next
End With
With ThisWorkbook.Worksheets("Sheet1")
.Cells(1, 1).Resize(idDict.Count, 1) = Application.Transpose(idDict.keys)
.Cells(1, 2).Resize(idDict.Count, 1) = Application.Transpose(idDict.items)
End With
.Quit
End With
End Sub
我正在尝试从此 website 中抓取数据,我需要 select 图片中的值(在本例中为“Москва, Новорязанское ш”):
我无法确定要使用的正确 "class",也不确定可以利用哪些函数来定位我要保留的特定值。 任何指导将不胜感激。
我拥有的代码:
Public Sub Selected()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.castorama.ru/building-materials/building-dry-materials-and-primers"
While .Busy Or .readyState <> 4: DoEvents: Wend
.document.getElementsByClassName("store-switcher__current-store-i").querySelector("[shop='Воронеж']").Click
Stop
End With
End Sub
如果您检查页面的 HTML,您会看到每个商店都有自己的 data-id
属性值。你可以select通过这个属性的值点击合适的店名节点:
Option Explicit
Public Sub MakeShopSelection()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate2 "https://www.castorama.ru/building-materials/building-dry-materials-and-primers"
While .Busy Or .readyState <> 4: DoEvents: Wend
.document.querySelector("[data-id='36']").Click
While .Busy Or .readyState <> 4: DoEvents: Wend
Stop
.Quit
End With
End Sub
下面为您列出ids:
Option Explicit
Public Sub ListIds()
Dim ie As SHDocVw.InternetExplorer, idDict As Object, shopNames As Object
Set ie = New SHDocVw.InternetExplorer
Set idDict = CreateObject("Scripting.Dictionary")
With ie
.Visible = True
.navigate2 "https://www.castorama.ru/building-materials/building-dry-materials-and-primers"
While .Busy Or .readyState <> 4: DoEvents: Wend
Dim i As Long
With .document
Set shopNames = .querySelectorAll(".shop__name")
For i = 0 To shopNames.Length - 1
idDict(Trim$(shopNames.Item(i).innertext)) = shopNames.Item(i).getAttribute("data-id")
Next
End With
With ThisWorkbook.Worksheets("Sheet1")
.Cells(1, 1).Resize(idDict.Count, 1) = Application.Transpose(idDict.keys)
.Cells(1, 2).Resize(idDict.Count, 1) = Application.Transpose(idDict.items)
End With
.Quit
End With
End Sub