简体   繁体   English

如何修复Excel VBA QueryTables不从网站提取数据

[英]How to fix Excel VBA QueryTables not pulling data from a website

I have used this code in the past to pull data from ESPN for the rosters in my fantasy baseball league. 过去,我曾使用此代码从ESPN提取我的幻想棒球联盟花名册中的数据。 I was able to get the rosters and put them all in one column in Excel. 我能够得到花名册并将它们全部放在Excel中的一栏中。 Then do some formatting. 然后进行一些格式化。 But now, data cannot be pulled. 但是现在,数据无法提取。 Nothing shows. 什么都没显示。 ESPN did change their site to look different so I am inclined to think that effected how this code can work. ESPN确实将其站点更改为外观不同,所以我倾向于认为这影响了此代码的工作方式。

What I have tried changing in the code so far: change the ".WebSelectionType" for all three types (xlSpecifiedTables, xlAllTables, xlEntirepage); 到目前为止,我一直尝试在代码中进行更改:更改所有三种类型(xlSpecifiedTables,xlAllTables,xlEntirepage)的“ .WebSelectionType”; tried different .WebTables values. 尝试了不同的.WebTables值。

-Will this ".QueryTable" command still work on this url? -这个“ .QueryTable”命令在这个网址上仍然可以使用吗? -Will I have to use a different command/code to scrape the table from this url? -我是否必须使用其他命令/代码从此url抓取表格?

Sheet11.Range("h:p").ClearContents  'clear old data
url = "URL;http://fantasy.espn.com/baseball/league/rosters?leagueId=101823"

With Sheet11.QueryTables.Add(Connection:= _
    url, Destination:=Range("$h$1"))
    .Name = "MyESPNRoster"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = False
    .RefreshPeriod = 0
    .WebSelectionType = xlSpecifiedTables
    .WebTables = "3,4,5,6,7,8,9,10,11,12,13,14"    'the table number to get the right table of data. there should be 12 rosters
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = True
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

Pretty much all that info (I think a lot more actually) is available in a json response from their API. 他们的API在json响应中几乎提供了所有这些信息(我认为实际上更多)。 Below is an example for the teams and names. 以下是团队和名称的示例。 You need to use a json parser . 您需要使用json解析器 After adding the .bas from the link supplied to your project, add the references shown below. 从提供给您的项目的链接中添加.bas之后,添加如下所示的引用。

Add a standard module to your project by opening the VBE with Alt + F11 , right click in project area and add module. 通过使用Alt + F11打开VBE,将标准模块添加到项目中,右键单击项目区域并添加模块。 Then paste code into module eg module 1. 然后将代码粘贴到模块(例如模块1)中。

In VBA Json structure the [] indicate collections accessed by index or For Each over. 在VBA Json结构中, []表示通过索引或For Each over访问的集合。 The {} are dictionaries accessed by key, everything else are string literals. {}是通过键访问的字典,其他所有内容都是字符串文字。

Option Explicit
'  VBE > Tools > References > Microsoft Scripting Runtime
Public Sub GetPlayers()
    Dim json As Object, ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    ws.Cells.ClearContents

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "http://fantasy.espn.com/apis/v3/games/flb/seasons/2019/segments/0/leagues/101823?view=mSettings&view=mRoster&view=mTeam&view=modular&view=mNav", False
        .send
        Set json = JsonConverter.ParseJson(.responseText)
    End With

    Dim item As Object, nextItem As Object, r As Long, c As Long
    c = 0
    For Each item In json("teams")
        r = 1: c = c + 1
        ws.Cells(r, c) = item("location") & " " & item("nickname")
        For Each nextItem In item("roster")("entries")
            r = r + 1
            ws.Cells(r, c) = nextItem("playerPoolEntry")("player")("fullName")
        Next
    Next
End Sub

Sample of json (1 player info): json样本(1个玩家信息):

The following is only a small sample of all the info retrieved for each team player (too much to show it all) 以下只是为每个团队成员检索的所有信息的一小部分(太多内容无法全部显示)

在此处输入图片说明


Sample of output: 输出样本:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM