如何在 VBA 中抓取网页时在 <b> 标签内获取数据
How to fetch data inside <b> tag while web scraping in VBA
我正在尝试从 this website 抓取数据。我已经编写了打开该网站然后搜索值的代码。单击搜索结果并打开最后一页,我必须从中选择详细信息。我需要选择
中红色提到的标签的详细信息
这是我打开所需页面的代码。我已使用 Link.click 打开所需的页面。之后我需要获取图像中提到的细节。请指教。
Sub hullByAshish()
Dim html, html1 As HTMLDocument
Dim ElementCol, ElementCol1 As Object
Dim Link As Object
Dim appIE As Object
Dim a As String
Dim i As Long
Dim objElement As Object
Dim objCollection As Object
Set appIE = CreateObject("internetexplorer.application")
a = "PONTOVREMON"
With appIE
.Navigate "https://www.marinetraffic.com/en/ais/index/search/all/keyword:" & a
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
Application.Wait (Now() + TimeValue("00:00:01"))
Set html = appIE.document
Set ElementCol = html.getElementsByTagName("a")
DoEvents
For Each Link In ElementCol
If Link.innerHTML = "PONTOVREMON" Then
Link.Click
End If
Next Link
End Sub
这是一种方法
Dim ie As Object, ieDoc As Object, lnk As Object
Sub hullByAshish()
Dim IMO As String, MMSI As String, GTon As String
Set ie = CreateObject("internetexplorer.application")
a = "PONTOVREMON"
With ie
.Navigate "https://www.marinetraffic.com/en/ais/index/search/all/keyword:" & a
.Visible = True
End With
Do While ie.readystate <> 4: Wait 5: Loop
DoEvents
Set ieDoc = ie.document
For Each lnk In ieDoc.getElementsByTagName("a")
If lnk.innerhtml = "PONTOVREMON" Then
lnk.Click
Exit For
End If
Next lnk
Do While ie.readystate <> 4: Wait 5: Loop
IMO = GetValue("IMO:")
MMSI = GetValue("MMSI:")
GTon = GetValue("Gross Tonnage:")
Debug.Print "IMO: " & IMO
Debug.Print "MMSI: " & MMSI
Debug.Print "Gross Tonnage: " & GTon
End Sub
Function GetValue(s As String) As String
GetValue = Trim(Split(Split(Split(Trim(Split(ie.document.body.innerhtml, s)(1)))(0), "<b>")(1), "</b>")(0))
End Function
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
截图
我正在尝试从 this website 抓取数据。我已经编写了打开该网站然后搜索值的代码。单击搜索结果并打开最后一页,我必须从中选择详细信息。我需要选择
中红色提到的标签的详细信息这是我打开所需页面的代码。我已使用 Link.click 打开所需的页面。之后我需要获取图像中提到的细节。请指教。
Sub hullByAshish()
Dim html, html1 As HTMLDocument
Dim ElementCol, ElementCol1 As Object
Dim Link As Object
Dim appIE As Object
Dim a As String
Dim i As Long
Dim objElement As Object
Dim objCollection As Object
Set appIE = CreateObject("internetexplorer.application")
a = "PONTOVREMON"
With appIE
.Navigate "https://www.marinetraffic.com/en/ais/index/search/all/keyword:" & a
.Visible = True
End With
Do While appIE.Busy
DoEvents
Loop
Application.Wait (Now() + TimeValue("00:00:01"))
Set html = appIE.document
Set ElementCol = html.getElementsByTagName("a")
DoEvents
For Each Link In ElementCol
If Link.innerHTML = "PONTOVREMON" Then
Link.Click
End If
Next Link
End Sub
这是一种方法
Dim ie As Object, ieDoc As Object, lnk As Object
Sub hullByAshish()
Dim IMO As String, MMSI As String, GTon As String
Set ie = CreateObject("internetexplorer.application")
a = "PONTOVREMON"
With ie
.Navigate "https://www.marinetraffic.com/en/ais/index/search/all/keyword:" & a
.Visible = True
End With
Do While ie.readystate <> 4: Wait 5: Loop
DoEvents
Set ieDoc = ie.document
For Each lnk In ieDoc.getElementsByTagName("a")
If lnk.innerhtml = "PONTOVREMON" Then
lnk.Click
Exit For
End If
Next lnk
Do While ie.readystate <> 4: Wait 5: Loop
IMO = GetValue("IMO:")
MMSI = GetValue("MMSI:")
GTon = GetValue("Gross Tonnage:")
Debug.Print "IMO: " & IMO
Debug.Print "MMSI: " & MMSI
Debug.Print "Gross Tonnage: " & GTon
End Sub
Function GetValue(s As String) As String
GetValue = Trim(Split(Split(Split(Trim(Split(ie.document.body.innerhtml, s)(1)))(0), "<b>")(1), "</b>")(0))
End Function
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
截图