【问题标题】:Use VBA to list all URL address of a web page使用 VBA 列出网页的所有 URL 地址
【发布时间】:2017-01-09 20:04:42
【问题描述】:

我使用下面的代码来加载网站http://www.flashscore.com/soccer/england/premier-league/results/

找到并点击“显示更多比赛”链接后,所有足球比赛都加载到浏览器中。

下面的代码将只给出匹配结果的前半部分,即在点击“显示更多匹配”链接之前显示的事件。

我的问题是如何列出所有事件的 URL 地址?

Sub Test_Flashscore()

Dim URL As String
Dim ie As New InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim dictObj As Object: Set dictObj = CreateObject("Scripting.Dictionary")
Dim tRowID As String

URL = "http://www.flashscore.com/soccer/england/premier-league/results/"

With ie
    .navigate URL
    .Visible = True
    Do Until .readyState = READYSTATE_COMPLETE: DoEvents: Loop
    Set HTMLdoc = .document
End With


For Each objLink In ie.document.getElementsByTagName("a")

   If Left(objLink.innerText, 4) = "Show" Or Left(objLink.innerText, 4) = "Arat" Then

        MsgBox "The link was founded!"
        objLink.Click

        Exit For

   End If

Next objLink


With HTMLdoc

    Set tblSet = .getElementById("fs-results")
    Set mTbl = tblSet.getElementsByTagName("tbody")(0)
    Set tRows = mTbl.getElementsByTagName("tr")
    With dictObj
        'If if value is not yet in dictionary, store it.
        For Each tRow In tRows
            'Remove the first four (4) characters.
            tRowID = Mid(tRow.ID, 5)
            If Not .Exists(tRowID) Then
                .add tRowID, Empty
            End If
        Next tRow
    End With
End With

i = 14
For Each Key In dictObj

    ActiveSheet.Cells(i, 2) = "http://www.flashscore.com/" & Key & "/#match-summary"
    i = i + 1

Next Key

Set ie = Nothing
MsgBox "Process Completed"

End Sub

【问题讨论】:

  • 似乎单击链接正在导航到新页面,因此您必须使用与加载初始页面类似的策略(READYSTATE_COMPLETE 等)等待它加载。

标签: vba list excel web-scraping


【解决方案1】:

您需要稍等片刻才能加载其余内容 - 单击链接会触发对服务器的 GET 请求,因此需要返回内容并且需要在页面上呈现内容才能加载抓住它。

【讨论】:

    【解决方案2】:

    单击该链接将带您进入固定装置。您可以将字典之前的所有内容替换为

    .navigate "https://www.flashscore.com/football/england/premier-league/fixtures/"
    

    即:

    Option Explicit
    Public Sub GetInfo()
        Dim IE As New InternetExplorer
        With IE
            .Visible = True
            .navigate "https://www.flashscore.com/football/england/premier-league/fixtures/"
    
            While .Busy Or .readyState < 4: DoEvents: Wend
    
            'other code...using dictionary
            '.Quit
        End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2011-03-14
      • 2018-09-13
      • 2011-12-27
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      相关资源
      最近更新 更多