Facebook Graph Search返回正确的JSON

时间:2013-03-21 16:29:39

标签: python json facebook-graph-api mechanize

我最近一直在使用机械化来爬网并做些什么。我决定写一些算法以图形方式显示我朋友之间的联系。每个朋友都是图中的一个节点,如果一个节点是另一个节点的朋友,那么它们之间就会有一条优势。基本上只是Facebook的共同朋友功能,但我希望它在一个节点代表图形像一个大蜘蛛网; networkx库和matplotlib可以实现这一点。无论如何,我有一个巨大的字典列表,每个朋友的名字和ID都是这样的:

friends = [
    {
     "name": "friendA",
     "ID": "99999999999"
    },
    {
     "name": friendB",
     "ID": "66666666666"
    }
]

所以我一直在使用mechanize的浏览器去图表搜索页面做我需要的事情:

import mechanize
import cookielib
cj = cookielibLWPCookieJar()
br = mechanize.Browser()
br.set_cookiejar(cj)
br.addheaders = [('User-Agent', 'Mozilla/5.0(X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.set_other_stuff(True)

# this will bring me to the Graph API, after I've logged in and stuff of course
# but for simplicity, we'll just assume I've already done that.
br.open("https://developers.facebook.com/tools/explorer/")

在我使用Graph API并获得访问令牌后,我可以执行以下操作。

# find our forms that we need to fill in with necessary information to get mutual friends

for form in br.forms():
    print form

1)
<GET https://developers.facebook.com/search application/x-www-form-urlencoded
<HiddenControl(path=) (readonly)>
<TextControl(selection=Search Facebook Developers)>
<SubmitButtonControl(<None>=) (readonly)>>
2)
<POST https://www.facebook.com/logout.php application/x-www-form-urlencoded
<HiddenControl(fb_dtsg=AQAudA2Y) (readonly)>
<HiddenControl(ref=ds) (readonly)>
<HiddenControl(h=AfeHIBr1AyKI-60f) (readonly)>
<SubmitControl(<None>=Log Out) (readonly)>>
3)
<GET https://developers.facebook.com/tools/explorer/ application/x-www-form-urlencoded
<TextControl(access_token=Paste in an existing Access Token or click 'Get Access Token' to generate one)>
<IgnoreControl(<None>=<None>)>
<SubmitControl(<None>=Debug) (readonly)>>

4)
<GET https://developers.facebook.com/tools/explorer/ application/x-www-form-urlencoded
<SelectControl(method=[, *GET, POST, DELETE])>
<TextControl(path=)>
<SubmitControl(<None>=Submit) (readonly)>>
5)
<GET https://developers.facebook.com/tools/explorer/ application/x-www-form-urlencoded
<HiddenControl(<None>=) (readonly)>
<TextareaControl(<None>=Input FQL query, <Tab> to autocomplete)>
<SubmitControl(<None>=Submit) (readonly)>>

正如您所看到的,有五种形式,其中只有两种需要填充信息。我们需要将访问令牌传递给表单3,它位于索引2处,我们需要将查询传递给表单4,它位于索引3.我刚刚生成了一个访问令牌以在另一个浏览器中使用并复制它用变量转换成python。如果您不熟悉机械化,我可以选择 br.select_form()

的表格
# select form 3 at index 2 to give access token
br.select_form(nr=2)
# to make sure all we need is writable
br.set_all_readonly(False)
access_token_var = 'AAACEdEose0cBAJ1FwFMNE7eLe4CZBPrz3q1AuKEZBsGKuwosCh8sPpHFLkOz2fZBmCV5uohZBI4SOTvUH4ilQEiPJE5KmEF73EdfzCP26jz7RFc9Kguu'
br['access_token'] = access_token_var

# now select the form to give it a path with what we need, which is form 4 at index 3
br.select_form(nr=3)
br["path"] = "/my_user_id/mutualfriends/friends_user_id"
br.submit()

现在,除了查看JSON之外,一切正常,表单选择,填充,甚至提交。当我尝试解析响应生成的JSON时,它在浏览器中完全可见为JSON库,它在html响应中找不到任何地方。包含它的元素是隐藏的。在浏览器中我得到了这个:

{
"data": [
    {
    "name": "some person",
    "ID": "3214564851"
    }
    ....
    ....
    ....
    ....
]
}

我只想从我给Graph API的路径中的相互朋友那里获得JSON响应。考虑到所述数据是API的全部目的,似乎使这些数据易于解析将是他们的优先事项。我不是通过Graph API迭代生成这些路径响应,而是单独传递路径并彻底复制和粘贴;我不会满足于此。我确信我有一种方法可以做到这一点,我还没有找到它,并且非常感谢你的帮助!

修改

ANSWER

我很快就找到了答案......

我正在寻找并最终找到的API是:

https://graph.facebook.com/me/mutualfriends/friend_id?access_token=accessToken

感谢。

0 个答案:

没有答案