Using Python for Facebook to search

为君一笑 提交于 2020-02-05 08:06:51

问题


I'm trying to use Python to search the Facebook Graph for Pages. When I use the Graph API Explorer on the Facebook webpage and I enter:

search?q=aquafresh&type=page

I get the results I'm looking for. When I do the same in Python (after installing the PythonForFacebook module):

post = graph.get_object("search?q=aquafresh&type=post")

I get:

facebook.GraphAPIError: Unsupported get request. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api

I believe I'm properly identifying with the token, I'm using the same one as the webpage and it works on the webpage. I'm also able to do basic queries in Python (e.g., querying for "me" works fine)


回答1:


I figured out a way to do this that doesn't involve the Facebook Graph API. Instead, use the Requests library:

import requests
token = "your_token"
query = "your_query"
requests.get("https://graph.facebook.com/search?access_token=" + token +  "&q=" + query + "&type=page")



回答2:


You can directly use search method of `GraphAPI

import facebook

TOKEN = "" # Your GraphAPI token here.

graph = facebook.GraphAPI(access_token=TOKEN, version="2.7")
posts = graph.search(q="aquafresh", type="post")

print posts['data']

Instead of:-

$ pip install facebook-sdk

Use this:-

pip install -e git+https://github.com/mobolic/facebook-sdk.git#egg=facebook-sdk



回答3:


What works for me is:

graph.request('search', {'q': 'aquafresh', 'type': 'page'})

That’s not exactly what you need, but when I try to search for posts (rather than pages) containing “aquafresh,” I get:

In [13]: graph.request('search', {'q': 'aquafresh', 'type': 'post'})
---------------------------------------------------------------------------
GraphAPIError                             Traceback (most recent call last)
<ipython-input-13-9aa008df54ba> in <module>()
----> 1 graph.request('search', {'q': 'aquafresh', 'type': 'post'})

/home/telofy/.buildout/eggs/facebook_sdk-0.4.0-py2.7.egg/facebook.pyc in request(self, path, args, post_args)
    296         except urllib2.HTTPError, e:
    297             response = _parse_json(e.read())
--> 298             raise GraphAPIError(response)
    299         except TypeError:
    300             # Timeout support for Python <2.6

GraphAPIError: (#11) Post search has been deprecated

(I wonder why deprecation doesn’t result in a mere warning.)

The request method seems to be missing from the documentation, but it’s documented in the code.




回答4:


This should work:

import facebook
aToken = "your access tocken"

graph = facebook.GraphAPI(access_token=aToken, version="2.10")
data = graph.search(
            q='aquafresh',
            type='post'
            )


来源:https://stackoverflow.com/questions/31147371/using-python-for-facebook-to-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!