Using Python for Facebook to search

11,721

Solution 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")

Solution 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

Solution 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.

Solution 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'
            )
Share:
11,721
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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)