2016-08-15 3 views
0

Ich versuche, ein Makro zu machen, um eine Tabelle von Fantasy-Fußball-Projektionen von ESPN zu kratzen. Ich habe den Code, um die Daten zu scrappen, aber ich konnte nicht herausfinden, wie ich die verschiedenen Versionen der URL durchlaufen muss, die ich brauche, um Daten von etwas anderem als der ersten Seite zu erfassen.Looping ein Web Scrapping VBA-Makro

Die URL lautet: "http://games.espn.com/ffl/tools/projections?&seasonTotals=true&seasonId=2016&slotCategoryId=0&startIndex=0"

Ich brauche die Werte für "slotCategoryID = 0" und "start = 0" iterieren. Jedes Mal, wenn die Webseite geladen wird, die Daten kopiert und an eine Tabelle in Excel angehängt werden.

slotCategoryID stellt die Spielerposition und sollte durch die Werte 0,2,4,6,16, & 17.

start einfach geht die Seite iterieren. Es ist 0 für Seite 1, 40 für Seite 2, 80 für Seite 3, usw.

Bitte helfen Sie!

Hier ist der Code, den ich bisher habe, die einmal die Datentabelle kopieren funktioniert:

Sub extractTablesData() 
'we define the essential variables 

Dim IE As Object 
Dim r As Integer, c As Integer, t As Integer, pos As Integer 
Dim elemCollection As Object 


'---- 
     'add the "Microsoft Internet Controls" reference in your VBA Project indirectly 
     Set IE = CreateObject("InternetExplorer.Application") 



     With IE 
     .Visible = True 
     .navigate "http://games.espn.com/ffl/tools/projections?&seasonTotals=true&seasonId=2016&slotCategoryId=0&startIndex=0" 

     ' we ensure that the web page downloads completely before we fill the form automatically 
     While IE.ReadyState <> 4 
     DoEvents 
     Wend 

     ' again ensuring that the web page loads completely before we start scraping data 
     Do While IE.busy: DoEvents: Loop 

     Set elemCollection = IE.Document.getElementsByTagName("TABLE") 

      For t = 0 To (elemCollection.Length - 1) 
       For r = 1 To (elemCollection(t).Rows.Length - 1) 
        For c = 0 To (elemCollection(t).Rows(r).Cells.Length - 1) 
         ThisWorkbook.Worksheets(1).Cells(r, c + 1) = elemCollection(t).Rows(r).Cells(c).innerText 
        Next c 
       Next r 
      Next t 

     End With 
     ' cleaning up memory 
     Set IE = Nothing 

'---- 
End Sub 
+0

Sie sagten es: Sie müssen iterieren. Also mach das. Aktualisieren Sie Ihre 'navigate' Zeichenfolge bei jeder Iteration. Oder noch besser, machen Sie 'extractTablesData' subgenerisch, so dass Sie eine beliebige URL übergeben können, und führen Sie dann die Iteration von einem treibenden Sub aus. – Marc

Antwort

0

Dies tun sollten, was Sie wollen !!

Sub Test() 

    Dim ie As Object 
    Dim i As Long 
    Dim strText As String 
    Dim doc As Object 
    Dim hTable As Object 
    Dim hBody As Object 
    Dim hTR As Object 
    Dim hTD As Object 
    Dim tb As Object 
    Dim bb As Object 
    Dim tr As Object 
    Dim td As Object 

    Dim y As Long, z As Long, wb As Excel.Workbook, ws As Excel.Worksheet 

    Set wb = Excel.ActiveWorkbook 
    Set ws = wb.ActiveSheet 

    Set ie = CreateObject("InternetExplorer.Application") 
    ie.Visible = True 

    y = 1 'Column A in Excel 
    z = 1 'Row 1 in Excel 
variable = 0 
Here: 

    ie.navigate "http://games.espn.com/ffl/tools/projections?&seasonTotals=true&seasonId=2016&slotCategoryId=0&startIndex=" & variable 

    Do While ie.Busy: DoEvents: Loop 
    Do While ie.ReadyState <> 4: DoEvents: Loop 

    Set doc = ie.document 
    Set hTable = doc.getElementsByClassName("playerTableTable tableBody") 

    For Each tb In hTable 

     Set hBody = tb.getElementsByTagName("tbody") 
     For Each bb In hBody 

      Set hTR = bb.getElementsByTagName("tr") 
      For Each tr In hTR 

       Set hTD = tr.getElementsByTagName("td") 
       y = 1 ' Resets back to column A 
       For Each td In hTD 
        ws.Cells(z, y).Value = td.innerText 
        y = y + 1 
       Next td 
       DoEvents 
       z = z + 1 
      Next tr 
      Exit For 
     Next bb 
    Exit For 
    Next tb 

variable = variable + 40 
GoTo Here: 
End Sub