【问题标题】:Trying to extract data from website into Excel. For loop not working试图将数据从网站提取到 Excel 中。 For循环不起作用
【发布时间】:2013-11-12 04:19:57
【问题描述】:

我正在尝试从以下网站提取有关 NFL 新兵的数据:

http://espn.go.com/college-sports/football/recruiting/rankings/_/class/2013

我需要访问每个位置并将信息复制粘贴/提取到 Excel 电子表格中。正如您在下面看到的,每个位置的 URL 的唯一区别是大写的 VARIABLE。我需要这个变量来从运动员转变为角卫再到外接手。

http://espn.go.com/college-sports/football/recruiting/playerrankings/_/position/VARIABLE/class/2013/view/position

这是我正在使用的代码:

Dim array_example(18) As String

Sub Macro1()


        array_example(0) = "athlete"
        array_example(1) = "cornerback"
        array_example(2) = "defensive-end"
        array_example(3) = "defensive-tackle"
        array_example(4) = "fullback"
        array_example(5) = "inside-linebacker"
        array_example(6) = "kicker"
        array_example(7) = "offensive-center"
        array_example(8) = "offensive-guard"
        array_example(9) = "outside-linebacker"
        array_example(10) = "offensive-tackle"
        array_example(11) = "quarterback-dual-threat"
        array_example(12) = "quarterback-pocket-passer"
        array_example(13) = "running-back"
        array_example(14) = "safety"
        array_example(15) = "tight-end-h"
        array_example(16) = "tight-end-y"
        array_example(17) = "wide-receiver"

        For i = 0 To 17

            LastUsedRow = ActiveSheet.Range("A1").Offset(Sheet1.Rows.Count - 1, 0).End(xlUp).Row

            LastEmptyRow = LastUsedRow + 1

            Cell = "A" & LastEmptyRow


            With ActiveSheet.QueryTables.Add(Connection:="URL;http://espn.go.com/college-sports/football/recruiting/playerrankings/_/position/" & array_example(i) & "/class/2013/view/position" & "", Destination:=Range("" & Cell & ""))
                .Name = "s"
                .FieldNames = True
                .RowNumbers = True
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .BackgroundQuery = True
                .RefreshStyle = xlInsertEntireCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = True
                .RefreshPeriod = 0
                .WebSelectionType = xlAllTables
                .WebFormatting = xlWebFormattingNone
                .WebPreFormattedTextToColumns = True
                .WebConsecutiveDelimitersAsOne = False
                .WebSingleBlockTextImport = False
                .WebDisableDateRecognition = False
                .WebDisableRedirections = False
                .Refresh BackgroundQuery:=True


            End With

        Next i

End Sub

我的问题是,每次我运行这段代码时,excel 都会卡住(有一个小圆盘一直为光标旋转)。当我按 Escape 停止代码时,我发现只有一个位置已复制到 Excel 电子表格中。您能否看一下我的代码,让我知道我可以更改哪些内容以循环遍历所有位置并将所有信息(一个接一个)复制到电子表格中?

非常感谢。

【问题讨论】:

  • 我试过你的代码,它正在工作
  • 你得到所有的观察了吗?还是只有大约 100 个(按等级衡量?当我尝试时,1 个职位只有 100 个。

标签: vba excel extract


【解决方案1】:

当我第一次运行代码时,我的经历与您描述的相同。我等了大约 2 分钟并终止了该进程,发现只有前 100 个已加载。

我进去并将这一行更改为false,以便我可以看到它正在加载。

.Refresh BackgroundQuery:=False

我还在Next i 之前添加了一个调试行,这样我就可以观察它是否真的遍历了所有地址。

    End With
  Debug.Print "next " & i
Next i

现在,当我运行它时,它只用了大约 30 秒就完成了所有 18 个地址。 excel 中超过 3000 行的结果。

然后我添加了一个简单的计时器来查看每个步骤需要多长时间。这次总共只用了 12 秒。

next 0 - 0 seconds
next 1 - 1 seconds
next 2 - 1 seconds
next 3 - 1 seconds
next 4 - 0 seconds
next 5 - 0 seconds
next 6 - 3 seconds
next 7 - 1 seconds
next 8 - 0 seconds
next 9 - 1 seconds
next 10 - 0 seconds
next 11 - 0 seconds
next 12 - 2 seconds
next 13 - 1 seconds
next 14 - 0 seconds
next 15 - 0 seconds
next 16 - 1 seconds
next 17 - 0 seconds
Total Time: 12

接下来,将 backgroundQuery 改回 true。计时器在不到 1 秒的时间内计数了所有 18 个结果,并且只显示了前 100 个结果。就像 excel 在设置所有连接之前运行代码一样,所以它只有足够的时间来设置第一个。

所以,我建议将后台查询设置为 false。每次我尝试的时间都在 12 到 30 秒之间。

在这里你可以看到它一直通过宽接收器。


嵌套循环问题

将外部循环写为年份循环。所以在For i = 0 To 17之前加上这个:

  For x = 2006 to 2013
    For i = 0 To 17

     '...continue your code

     ' Change With line to this:
     With ActiveSheet.QueryTables.Add(Connection:="URL;http://espn.go.com/college-sports/football/recruiting/playerrankings/_/position/" & array_example(i) & "/class/" & CStr(x) & "/view/position" & "", Destination:=Range("" & Cell & ""))

     '...continue your code

   Next i
  Next x
End Sub

【讨论】:

  • 非常感谢。这对我很有用。我现在正在复制 2006-2014 年的数据库。
  • 波特兰跑者,再次感谢您的帮助。我不仅需要循环浏览每一年,而且还需要将每年的数据导入单独的工作表中。例如,我想要 sheet1 中 2003 年的所有数据点和 sheet2 中 2004 年的所有数据点。有没有办法将此信息包含在您上面提到的嵌套循环中?我试图通过将 ActiveSheet 更改为每个 x 从一张表更改为另一张表的变量来弄乱“With”行,但 With 行不接受变量或字符串代替 ActiveSheet
  • 是的,可以做到。您想要以当前年份命名的工作表,还是它们是具有不同名称的现有工作表?
  • 我希望以当前年份命名这些工作表。谢谢
【解决方案2】:

您的代码运行良好。也许您的互联网连接速度很慢/没有网络连接来获取数据。

这就是它的样子

我获得了 100 个 WR。如果我只是第一次运行循环并停止它,我会得到 100 ATH。

看起来您的 for 循环正在运行并将您的 QueryTable 放在一行中,然后在数据填充之前将下一个放在它下面的一行中,可能会覆盖它。我会把每一个都放在一个单独的工作表中。

【讨论】:

  • 感谢您的评论。如果你得到了所有的观察,你能告诉我吗?如果是,需要多长时间?当我检查时,我只得到了 100 个观察值(按等级衡量)。我希望得到所有位置的大约 100 个观察值,加起来大约 900 个 obs。请告诉我。
  • 感谢您的更新。我需要一个工作表中一年的所有数据(在这种情况下是 2013 年)。最好,我希望填写一个位置的观察结果,然后在其前面的行中填写另一个位置。您知道实现这一点的方法吗?
  • @SidharthMoktan 如果您确定只有 100 个,那么您可以每 110 行间隔一次表格。不过,这不是很程序化。您搜索最后一行并向上移动的方式是要走的路,除了您需要等待每个 QueryTable 完成才能进入下一个。没有令人耳目一新的属性(在 excel vba 中)我可以找到知道停止等待。
  • 你认为如果我使用时间延迟会有帮助吗?我在 StackOverflow 中环顾四周,发现了以下命令。 Application.Wait(现在 + TimeValue(“0:00:01”))。我可以将它放在 End With 语句之后大约 10 秒吗?
  • 我猜你可以,但你不知道需要多长时间。如果连接在某个特定时间很慢怎么办?我没有很好的解决方案。但是您最好的选择是等待查询完成刷新或根据对数据大小的了解来分隔行。 @Portland Runner 的回答看起来很有希望
猜你喜欢
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多