如何按类别和/或位置解析 Facebook 页面的 FQL 响应? (Python)
长期倾听者,第一次调用者......
我从 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,我还是新手,需要在这些方面的帮助:
我只想请求符合以下条件的页面:匹配某些类别,例如“本地业务”,但因为“类别”是一个列表(并且它没有在 FB 表中建立索引),所以我很难弄清楚它;我假设这是不可能的。
因此,我检索了一个人喜欢的所有页面的全部内容,然后我需要对其进行整理。这是我在做时遇到的问题:
如何操作结果(按类别和/或位置过滤,这都是列表)并将它们以可读格式发送到我的 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:
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您只是想在这里过滤最终结果?如果还没有,请继续将 json 字符串转换为 python 对象
loads
代表 load string,这是您将从服务返回的内容。从这里您有几个选择。如果您事先知道所需的类别,一种选择是使用内置的过滤器
。以下面的数据集为例,您可以过滤仅包含
b
类别的结果,如果您想要一个可重用的函数来过滤不同的类别
,您可以执行类似的操作最后,您可以只使用列表理解,根据需要内联或在 filter_objs 函数中。
所以有很多种方法来过滤结果,但第一步是使用 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
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 builtinfilter
if you know before hand what categories you want. Take for example the following data setYou could filter results that only contain the
b
category like soif you want a reusable function to filter for different categories you could do something like
And lastly, you could just use a list comprehension, either inline as needed or in the filter_objs function.
So there's a number of ways to filter the result, but the first step is to use
json.loads