从单个网站的多个页面导入数据

Importing data from multiple pages of a single website

我是 VBA 的新手,在弄清楚如何从网站 boxofficemojo.com 中提取数据方面遇到了很多麻烦。我正在尝试提取 2010-2015 年的每周数据。所以我找到了一个代码,它按照相同的方式做了一些事情,并对其进行了更改以满足我的需要。如下

Sub Movies()
Dim nextRow As Integer, i As Integer
Application.ScreenUpdating = False
Application.DisplayStatusBar = True
For i = 1 To 52 
Application.StatusBar = "Processing Page " & i
nextRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.boxofficemojo.com/weekly/chart/?yr=2015&wk=&p=.htm" & i, _
Destination:=Range("A" & nextRow))

.Name = "weekly/chart/?yr=2015&wk=&p=.htm"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingAll
.WebTables = "5"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = True
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
ThisWorkbook.Save
Next i
Application.StatusBar = False
End Sub

然而,它并没有提取 2015 年第 1 - 52 周的数据,而是继续提取 2016 年最后一周的数据并重复 52 次。我不确定这里出了什么问题,非常感谢任何帮助。

感谢您的努力。

你离那里很近了。

QueryTables.Add方法中的url字符串调整为:

http://www.boxofficemojo.com/weekly/chart/?yr=2015&wk=" & i & "&p=.htm"

既然你说你想要 2010 年到 2015 年。你可以将现有循环包装在另一个循环中 For x = 2010 to 2015 然后将 URL 修改为:

http://www.boxofficemojo.com/weekly/chart/?yr=" & x & "&wk=" & i & "&p=.htm"