使用Microsoft Excel for Facebook Graph数据发出GET请求

时间:2014-09-13 11:01:36

标签: facebook-graph-api excel-vba get vba excel

我试图在Excel中使用GET请求从Facebook Graph获取元数据

我正在使用Excel 2003

我在Facebook Graph中使用了5,000个app_scoped_user_id&#39。我想创建一个宏来从每个图表中提取信息,该图表位于https://graph.facebook.com/app_scoped_user_id。每个Graph都是一个简单的HTML页面,内容如下

{
    "id": "XXXXXXXXXXXX",
    "first_name": "XXX",
    "gender": "XXX",
    "last_name": "XXX",
    "link": "https://www.facebook.com/XXX",
    "locale": "id_ID",
    "name": "XXX",
    "username": "XXX"
}

我希望将此HTML用于解析,以便将每个字段放入Excel中app_scoped_user_id旁边的单元格中。有没有办法在Excel中作为GET请求执行此操作,以根据第一列中的ID动态地使每一行看起来像这样:

id | first_name | last_name | gender | link | locale | username

或只是

id | everything as a text string

我不确定如何为此编写脚本,我试图查找Microsoft.XMLHTTP以在VBA编辑器中创建模块但不知道从哪里开始

谢谢

1 个答案:

答案 0 :(得分:0)

尝试这个,有一个示例获得前100个ID。 IMO在某些情况下没有必要涉及JSON解析,Split函数会这样做。

Sub GetSomeGraphData()
    Dim lId, lRow, aPattern
    aPattern = Array("id", "first_name", "last_name", "gender", "link", "locale", "username")
    lRow = 1
    ' example getting first 100 records
    For lId = 1 To 100
        If WriteGraphData(lId, lRow, aPattern) Then lRow = lRow + 1
        DoEvents
    Next
End Sub

Function WriteGraphData(lId, lRow, aPattern)
    ' lId - ID you want to get data for
    ' lRow - Excel sheet row where to write data
    ' aPattern - array containing the parameters to be written, in order
    ' returns: True - if at least one cell was populated with a value, False - in case of blank line
    Dim oXMLHttp, lCol, sProp, aTemp
    Set oXMLHttp = CreateObject("Microsoft.XMLHttp")
    oXMLHttp.Open "GET", "https://graph.facebook.com/" & lId, False
    oXMLHttp.Send ("")
    lCol = 1
    WriteGraphData = False
    For Each sProp In aPattern
        aTemp = Split(oXMLHttp.ResponseText, """" & sProp & """: """)
        If UBound(aTemp) = 1 Then
            aTemp = Split(aTemp(1), """")
            Cells(lRow, lCol).Value = aTemp(0)
            WriteGraphData = True
        End If
        lCol = lCol + 1
    Next
    Set oXMLHttp = Nothing
End Function