【问题标题】:How to scrape data from Datastore.prime using vba如何使用 vba 从 Datastore.prime 中抓取数据
【发布时间】:2016-03-31 13:07:30
【问题描述】:

我正在尝试从这个网站http://www.whoscored.com/regions/252/tournaments/2/england-premier-league抓取数据

当我使用检查元素时,我看到数据是表格格式,如下图所示。

源代码有这种格式的数据。

DataStore.prime('stagefixtures', $.extend({ stageId: 12496, isAggregate: false }, calendar.parameter()), [[959688,1,'Monday, Dec 21 2015','20:00 ',13,'阿森纳',0,167,'曼城',0,'2 : 1','2 : 0',1,1,'FT','1',0,1,112,0] ,[959683,4,'2015 年 12 月 26 日星期六','12:45',96,'斯托克',0,32,'曼联',0,'vs',,0,1,,'-1 ',0,1,13,0] ,[959615,4,'Saturday, Dec 26 2015','15:00',24,'阿斯顿维拉',0,29,'西汉姆',0,'vs',,0,1,,'- 1',0,1,6,0] ,[959625,4,'Saturday, Dec 26 2015','15:00',183,'伯恩茅斯',0,162,'水晶宫',0,'vs',,0,1,,'-1', 0,1,10,0] ,[959635,4,'2015 年 12 月 26 日星期六','15:00',15,'切尔西',0,27,'沃特福德',0,'vs',,0,1,,'-1' ,0,1,15,0] ,[959645,4,'2015 年 12 月 26 日星期六','15:00',26,'利物浦',0,14,'莱斯特',0,'vs',,0,1,,'-1' ,0,1,15,0] ,[959655,4,'Saturday, Dec 26 2015','15:00',167,'曼城',0,16,'桑德兰',0,'vs',,0,1,,'-1 ',0,1,4,0​​] ,[959691,4,'Saturday, Dec 26 2015','15:00',259,'Swansea',0,175,'West Bromwich Albion',0,'vs',,0,1,,'-1' ,0,1,5,0] ,[959698,4,'Saturday, Dec 26 2015','15:00',30,'Tottenham',0,168,'Norwich',0,'vs',,0,1,,'-1',0 ,1,8,0] ,[959665,4,'2015 年 12 月 26 日星期六','17:30',23,'纽卡斯尔联队',0,31,'埃弗顿',0,'vs',,0,1,,'-1 ',0,1,7,0] ,[959674,4,'2015 年 12 月 26 日星期六','19:45',18,'南安普顿',0,13,'阿森纳',0,'vs',,0,1,,'-1' ,0,1,11,0] ]);

此代码应该从表格格式中抓取数据,但在这种情况下我不知道该怎么做。

Option Explicit

Sub WeeklyFixtures()


 Dim IE As Object, obj As Object

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

 Set IE = CreateObject("InternetExplorer.Application")

 With IE
 .Visible = True
 .navigate ("http://www.whoscored.com/regions/252/tournaments/2/england-premier-league")
While IE.ReadyState <> 4
DoEvents
Wend

 Do While IE.busy: DoEvents: Loop

ThisWorkbook.Sheet1.Clear

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

    For t = 0 To (elemCollection.Length - 1)

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

 End With

 Set IE = Nothing

 End Sub

【问题讨论】:

  • 为什么要使用 VBA 来检索数据?一个简单的web query 在我的电脑上工作得很好。只需在Data 菜单上转到Get External Data 并选择From Web。您可以在此处输入您提供给我们的 URL,然后选择您要检索的表格。您甚至可以将查询设置为每xx 秒刷新一次。
  • @Ralph 我在桌子附近找不到next -&gt; 符号
  • 当我尝试在 IE 11 中打开站点时,它没有完成加载,表是空的。在 Firefox 中它运行良好。该网站在 IE 中是否适合您?在您使用 Firefox 的屏幕截图中,您是否尝试在 IE 中手动打开页面?
  • @dee 在 IE 中我得到了所有的表格,但是在 excel 中查询时我没有下一个标志以及表格。
  • id="tournament-fixture" 正在动态创建。从服务器下载的网页HTML代码不包含它。

标签: javascript vba excel web-scraping


【解决方案1】:

试试这个代码:

Option Explicit

Sub GetWhoscoredData()

    Dim strCont, arrRows, strComma, arrQuots, i, arrCols

    With CreateObject("Microsoft.XMLHTTP")
        .Open "GET", "http://www.whoscored.com/regions/252/tournaments/2/england-premier-league", False
        .Send
        strCont = .ResponseText
    End With

    strCont = Split(strCont, "'stagefixtures'")(1)
    strCont = Split(strCont, "[[")(1)
    strCont = Split(strCont, "]);")(0)
    strCont = Replace(strCont, vbCrLf, "")
    strComma = Mid(CreateObject("Scriptlet.TypeLib").GUID, 2, 36)
    arrQuots = Split(strCont, "'")
    For i = 1 To UBound(arrQuots) Step 2
        arrQuots(i) = Replace(arrQuots(i), ",", strComma)
    Next
    strCont = Join(arrQuots, "")
    arrRows = Split(strCont, "],[")
    For i = 0 To UBound(arrRows)
        arrCols = Split(arrRows(i), ",")
        Cells(i + 1, 1).Value = Replace(arrCols(2), strComma, ",")
        Cells(i + 1, 2).Value = Replace(arrCols(3), strComma, ",")
        Cells(i + 1, 3).Value = Replace(arrCols(14), strComma, ",")
        Cells(i + 1, 4).Value = Replace(arrCols(5), strComma, ",")
        Cells(i + 1, 5).NumberFormat = "@"
        Cells(i + 1, 5).Value = Replace(arrCols(10), strComma, ",")
        Cells(i + 1, 6).Value = Replace(arrCols(8), strComma, ",")
    Next
    Cells.Columns.AutoFit

End Sub

它给我的输出如下:

【讨论】:

    【解决方案2】:

    您有 Excel 2016 吗?在那里你应该能够得到它:Data --> New Query --> From Other Sources --> From Web。在那里您可以输入您的网址。如果您记录所有这些,您甚至可以获得相应的 VBA 代码。

    【讨论】:

    • 你能分享你的代码吗?我在桌子上找不到我需要的黄色下一个标志。也是您正在使用的 excel 或 access 吗?
    • 我使用的是 Excel 2013
    猜你喜欢
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2021-11-08
    相关资源
    最近更新 更多