如何按类别和/或位置解析 Facebook 页面的 FQL 响应? (Python)

发布于 2024-12-22 12:01:15 字数 1378 浏览 0 评论 0原文

长期倾听者,第一次调用者......

我从 Facebook Open Graph API 查询中得到了很好的结果:

fbtest = graph.request("/fql", {"q": "SELECT name, page_id, categories, location 
FROM page WHERE page_id IN (SELECT page_id FROM page_fan WHERE uid=me())"})

尽管我已经学习了 Zed 的精彩课程 以艰难的方式学习Python,我还是新手,需要在这些方面的帮助:

  1. 我只想请求符合以下条件的页面:匹配某些类别,例如“本地业务”,但因为“类别”是一个列表(并且它没有在 FB 表中建立索引),所以我很难弄清楚它;我假设这是不可能的。

  2. 因此,我检索了一个人喜欢的所有页面的全部内容,然后我需要对其进行整理。这是我在做时遇到的问题:

如何操作结果(按类别和/或位置过滤,这都是列表)并将它们以可读格式发送到我的 fbtest.html 文件?

目前我只是用 HTML 渲染 fbtest 的输出;

self.render("test.html", fbtest=fbtest)

这是相当丑陋的:

fbtest: {u'data': [{u'page_id': 8495417058L, u'name': u'Mat Zo', u'类别': [], u'位置': {u'街道': u'', u'zip': u''}}, {u'page_id': 9980651805L, u'name': u'deadmau5', u'categories': [], u'位置':{u'街道':u'',u'zip':u''}},{u'page_id':6209079710L, u'name': u'Ultra Records', u'categories': [], u'location': {u'street': u'', u'zip': u''}}, {u'page_id': 12609724042L, u'name': u'Oceanlab', u'categories': []、u'location': {u'street': u''、u'zip': u''}} 等

一旦我尝试操作列表,我就可以发送单个结果(例如匹配名称=“thesocialbusiness”的页面),但不是我正在寻找的一系列结果。我的愿景是拥有一个漂亮的图画书缩略图页面,并按位置进行分类和排序。

谢谢,节日快乐,

-詹姆斯

Long time listener, first time caller..

I'm getting a good result from my Facebook Open Graph API query:

fbtest = graph.request("/fql", {"q": "SELECT name, page_id, categories, location 
FROM page WHERE page_id IN (SELECT page_id FROM page_fan WHERE uid=me())"})

And although I've taken Zed's great course on Learning Python the Hard Way, I'm still green and need help on these fronts:

  1. I'd love to ONLY request pages that match certain categories, such as "Local Business" but because 'categories' is a list (and it's not indexed in the FB table), I'm having a tough time figuring it out; I'm assuming it's not possible.

  2. So I retrieve the full blast of ALL pages that a person likes, and I need to sort it out afterwards. Here's what I'm having trouble doing:

How do I manipulate the results (filtering by category and/or location, which are both lists) and send them in a readable format to my fbtest.html file?

Currently I am just rendering the output of fbtest in HTML;

self.render("test.html", fbtest=fbtest)

which is pretty ugly:

fbtest: {u'data': [{u'page_id': 8495417058L, u'name': u'Mat Zo',
u'categories': [], u'location': {u'street': u'', u'zip': u''}},
{u'page_id': 9980651805L, u'name': u'deadmau5', u'categories': [],
u'location': {u'street': u'', u'zip': u''}}, {u'page_id': 6209079710L,
u'name': u'Ultra Records', u'categories': [], u'location': {u'street':
u'', u'zip': u''}}, {u'page_id': 12609724042L, u'name': u'Oceanlab',
u'categories': [], u'location': {u'street': u'', u'zip': u''}}, etc

And as soon as I try to manipulate the list, I can send a single result (like the page matching name = "thesocialbusiness") but not a series of results that I'm looking for. My vision is to have a nice picture-book thumbnail view of pages that is categorized and sorted by location.

Thanks and happy holidays,

-James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2024-12-29 12:01:15

如果我理解正确的话,您只是想在这里过滤最终结果?如果还没有,请继续将 json 字符串转换为 python 对象

import json
resp = json.loads(fql_resp)
data = resp['data']

loads 代表 load string,这是您将从服务返回的内容。从这里您有几个选择。如果您事先知道所需的类别,一种选择是使用内置的过滤器。以下面的数据集为例,

objs = [{'_id': 1, 'categories': ['a', 'b', 'c']},
        {'_id': 2, 'categories': ['b']},
        {'_id': 3, 'categories': ['c']}]

您可以过滤仅包含 b 类别的结果,

def f(obj):
    return 'b' in obj['categories']

filtered_objs = filter(f, objs)
print(filtered_objs)
# outputs:
# [{'_id': 1, 'categories': ['a', 'b', 'c']},
#  {'_id': 2, 'categories': ['b']}]

如果您想要一个可重用的函数来过滤不同的类别

def filter_objs(objs, category):
    result = []
    for obj in objs:
        if category in obj['categories']:
            result.append(category)
    return result

filtered_objs = filter_objs(objs, 'b')

,您可以执行类似的操作最后,您可以只使用列表理解,根据需要内联或在 filter_objs 函数中。

filtered_objs = [obj for obj in objs if 'b' in obj['categories']]

所以有很多种方法来过滤结果,但第一步是使用 json.loads

If I understand correctly, you're just looking to filter the end result here? If you haven't already, go ahead and convert the json string to a python object

import json
resp = json.loads(fql_resp)
data = resp['data']

The loads stands for load string which is what you'll be getting back from the service. From here you have a few options. One choice is to use the builtin filter if you know before hand what categories you want. Take for example the following data set

objs = [{'_id': 1, 'categories': ['a', 'b', 'c']},
        {'_id': 2, 'categories': ['b']},
        {'_id': 3, 'categories': ['c']}]

You could filter results that only contain the b category like so

def f(obj):
    return 'b' in obj['categories']

filtered_objs = filter(f, objs)
print(filtered_objs)
# outputs:
# [{'_id': 1, 'categories': ['a', 'b', 'c']},
#  {'_id': 2, 'categories': ['b']}]

if you want a reusable function to filter for different categories you could do something like

def filter_objs(objs, category):
    result = []
    for obj in objs:
        if category in obj['categories']:
            result.append(category)
    return result

filtered_objs = filter_objs(objs, 'b')

And lastly, you could just use a list comprehension, either inline as needed or in the filter_objs function.

filtered_objs = [obj for obj in objs if 'b' in obj['categories']]

So there's a number of ways to filter the result, but the first step is to use json.loads

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文