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
循环Web抓取VBA宏_Vba_Excel_Web Scraping_Macros - Fatal编程技术网

循环Web抓取VBA宏

循环Web抓取VBA宏,vba,excel,web-scraping,macros,Vba,Excel,Web Scraping,Macros,我正试着制作一个宏,从ESPN上抓取一张关于梦幻足球的预测表。我已经有了用于抓取数据的代码,但是我还没有弄清楚如何迭代不同版本的URL,以便从除第一页以外的任何地方捕获数据 网址是:“ 我需要迭代“slotCategoryID=0”和“startIndex=0”的值。每次加载网页、复制数据并将其附加到excel中的表中时 slotCategoryID表示玩家的位置,应该遍历值0、2、4、6、16和17 startIndex只是简单地推进页面。第一页是0,第二页是40,第三页是80,等等 请帮忙

我正试着制作一个宏,从ESPN上抓取一张关于梦幻足球的预测表。我已经有了用于抓取数据的代码,但是我还没有弄清楚如何迭代不同版本的URL,以便从除第一页以外的任何地方捕获数据

网址是:“

我需要迭代“slotCategoryID=0”和“startIndex=0”的值。每次加载网页、复制数据并将其附加到excel中的表中时

slotCategoryID表示玩家的位置,应该遍历值0、2、4、6、16和17

startIndex只是简单地推进页面。第一页是0,第二页是40,第三页是80,等等

请帮忙

以下是我到目前为止的代码,它可以复制数据表一次:

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
Sub-extractTablesData()
“我们定义了基本变量
模糊的物体
Dim r为整数,c为整数,t为整数,pos为整数
作为对象的暗元素集合
'----
'在VBA项目中添加“Microsoft Internet控件”引用
设置IE=CreateObject(“InternetExplorer.Application”)
与IE
.Visible=True
.导航“http://games.espn.com/ffl/tools/projections?&seasonTotals=true&seasonId=2016&slotCategoryId=0&startIndex=0"
“在我们自动填写表单之前,我们确保网页完全下载
而IE.ReadyState 4
多芬特
温德
再次确保网页在开始抓取数据之前完全加载
在忙的时候做
Set elemCollection=IE.Document.getElementsByTagName(“表”)
对于t=0到(elemCollection.Length-1)
对于r=1到(elemCollection(t).Rows.Length-1)
对于c=0到(elemCollection(t).Rows(r).Cells.Length-1)
此工作簿.工作表(1).单元格(r,c+1)=元素集合(t).行(r).单元格(c).内部文本
下一个c
下一个r
下一个t
以
“清理内存
设置IE=无
'----
端接头

这应该是您想要的

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
子测试()
模糊的物体
我想我会坚持多久
将strText设置为字符串
Dim doc作为对象
可调为对象
作为物体的物体
作为对象的Dim hTR
作为对象的Dim hTD
将tb作为对象
将bb作为对象
作为对象的Dim tr
将td作为对象
将y标注为长,z标注为长,wb标注为Excel.工作簿,ws标注为Excel.Worksheet
设置wb=Excel.active工作簿
设置ws=wb.ActiveSheet
设置ie=CreateObject(“InternetExplorer.Application”)
可见=真实
y=1'Excel中的A列
z=1'Excel中的第1行
变量=0
在这里:
即“导航”http://games.espn.com/ffl/tools/projections?&seasonTotals=true&seasonId=2016&slotCategoryId=0&startIndex=“&变量
在忙的时候做
Do While ie.ReadyState 4:DoEvents:Loop
Set doc=ie.document
Set hTable=doc.getElementsByClassName(“playerTableTable表体”)
对于HTTable中的每个tb
设置hBody=tb.getElementsByTagName(“tbody”)
对于hBody中的每个bb
设置hTR=bb.getElementsByTagName(“tr”)
对于hTR中的每个tr
设置hTD=tr.getElementsByTagName(“td”)
y=1'重置回A列
对于hTD中的每个td
ws.Cells(z,y).Value=td.innerText
y=y+1
下一个td
多芬特
z=z+1
下一个tr
退出
下一个bb
退出
下一个结核病
变量=变量+40
转到这里:
端接头

你说过了:你需要迭代。那就这么做吧。在每次迭代中更新
导航
字符串。或者更好的方法是,将
extractTablesData
子类化,这样就可以传入任何URL,然后从驱动子类进行迭代。