Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA列出网页的所有URL地址_Vba_List_Excel_Web Scraping - Fatal编程技术网

使用VBA列出网页的所有URL地址

使用VBA列出网页的所有URL地址,vba,list,excel,web-scraping,Vba,List,Excel,Web Scraping,我使用下面的代码加载网站 在我找到并点击“显示更多比赛”链接后,所有足球比赛都会加载到浏览器中 下面的代码将仅给出前一半匹配的结果,即在按下“显示更多匹配”链接之前显示的事件 我的问题是如何列出所有事件URL地址 Sub Test_Flashscore() Dim URL As String Dim ie As New InternetExplorer Dim HTMLdoc As HTMLDocument Dim dictObj As Object: Set dictObj = Create

我使用下面的代码加载网站

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

下面的代码将仅给出前一半匹配的结果,即在按下“显示更多匹配”链接之前显示的事件

我的问题是如何列出所有事件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

您需要等待一段时间,以便加载其余内容-单击链接会向服务器发出GET请求,因此服务器需要返回内容,并且需要在页面上呈现内容,然后才能抓取它。

单击该链接将带您进入Fixture。你可以用字典替换之前的所有内容

.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
选项显式
公共子GetInfo()
Dim IE成为新的InternetExplorer
与IE
.Visible=True
.导航“https://www.flashscore.com/football/england/premier-league/fixtures/"
当.Busy或.readyState<4:DoEvents:Wend时
'其他代码…使用字典
”“退出
以
端接头

单击链接似乎是导航到新页面,因此您必须使用与加载初始页面(READYSTATE\u COMPLETE等)类似的策略等待加载。