如何在VBA中进行Web报废时获取<b>标记内的数据

时间:2017-06-25 08:22:53

标签: excel vba excel-vba web-scraping innerhtml

我正在尝试从this website中删除数据。我已经编写了打开该网站的代码,然后搜索了一个值。点击搜索结果并打开我必须选择详细信息的最后一页。我需要选择

中红色标记的详细信息

this image

这是我打开所需页面的代码。我用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

1 个答案:

答案 0 :(得分:3)

这是一种方式

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

<强>截图

enter image description here