2015-12-25 53 views
2

我试图从本网站http://www.whoscored.com/regions/252/tournaments/2/england-premier-league如何从Datastore.prime使用VBA

抽取数据当我使用检查元素我看到的数据以表格形式如上图下方抽取数据。

enter image description here

的源代码在这个格式的数据。

enter image description here

DataStore.prime( 'stagefixtures',$ .extend({级ID:12496,isAggregate:假},calendar.parameter()),〔959688,1,“周一,十二月'2015','20:00',13,'Arsenal',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'2015年12月26日星期六'''15:00''24'Aston Villa',0,29'West Ham',0,'vs',, 0,1 ,,' - 1',0,1,6,0] ,[959625,4,'2015年12月26日星期六','15:00',183 ,'伯恩茅斯',0,162,'水晶宫',0,'vs',, 0,1 ,,' - 1',0,1,10,0] ,[959635,4,'2015年12月26日星期六','15:00',15,'Chelsea',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,'2015年12月26日星期六','15:00',167'曼城',0, 16,'桑德兰',0,'vs',, 0,1 ,,' - 1',0,1,4,0​​] ,[959691,4,'2015年12月26日星期六','15:00 ',259,'Swansea',0,175,'West Bromwich Albion',0,'vs',, 0,1 ,,' - 1',0,1,5,0] ,[959698,4',Saturday ,'2015年12月26日','15:00',30'托特纳姆',0,168'诺维奇',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 
+0

你想使用VBA来检索数据,为什么呢?一个简单的'网络查询'工作在我的电脑上就好了。只需点击“数据”菜单上的“获取外部数据”并选择“从网站”。在那里你可以输入你给我们的URL并选择你想要检索的表格。你甚至可以设置查询来刷新每个'xx'秒。 – Ralph

+0

@Ralph我在表格 – Anuj

+0

附近找不到'next - >'符号当我尝试在IE 11中打开网站时,它并未完成加载,表格为空。在Firefox中,它工作正常。该网站在IE中适合你吗?在使用Firefox的屏幕截图中,您是否尝试手动打开IE中的页面? – dee

回答

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 

它给我的输出如下:

output

1

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

enter image description here

+0

你能分享你的代码吗?我找不到我需要的桌子上的黄色下一个标志。你使用的是excel还是access? – Anuj

+0

http://imgur.com/w19rTu7请看看屏幕截图 – Anuj

+0

我正在使用Excel 2013 – Anuj