ich würde mir gerne alle Beiträge von einer öffentlichen Facebook Seite und die dann z.B. nach Stichwörtern durchsuchen oder mit regex matchen. Einen echten Sinn hat das ganze zunächst eigentlich nicht, das ist eher nur eine Spielerei. Ich habe dazu 2 Möglichkeiten gefunden, leider funktionieren allerdings beide nicht.
Variante 1 nach http://simplebeautifuldata.com/2014/08/ ... er-part-1/
Zunächst der Code (zumindest der erste Zwischenschritt). Wenn ich den ausführe bekomme ich aber den HTTP Error 400: Bad Request
Code: Alles auswählen
import urllib2
import json
def main():
#to find username go to FB page, copy the end of URL
#e.g. http://facebook.com/walmart, walmart is the username
list_companies = ["Walmart"]
graph_url = "http://graph.facebook.com/"
for company in list_companies:
#make graph api url with company username
current_page = graph_url + company
#open public page in facebook graph api
web_response = urllib2.urlopen(current_page)
readable_page = web_response.read()
json_fbpage = json.loads(readable_page)
#print page data to console
print company + " page"
print json_fbpage["id"]
print json_fbpage["likes"]
print json_fbpage["talking_about_count"]
print json_fbpage["username"]
print " "
if __name__ == '__main__':
main()
Code: Alles auswählen
Traceback (most recent call last):
File "fb.py", line 29, in <module>
main()
File "fb.py", line 15, in main
web_response = urllib2.urlopen(current_page)
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 435, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 548, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 473, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 556, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
Ich glaube allerdings nicht dass das das richtige für mich ist. In den Kommentaren wird erwähnt dass man seine app bei facebook anmelden muss aber ich will ja nur ein lokales Skript. Trotzdem der Code
Code: Alles auswählen
from django.conf import settings
import facebook
import requests
class FacebookFeed:
#settings.configure()
token_url = 'https://graph.facebook.com/oauth/access_token'
params = dict(client_id=settings.SOCIAL_AUTH_FACEBOOK_KEY, client_secret=settings.SOCIAL_AUTH_FACEBOOK_SECRET, grant_type='client_credentials')
@classmethod
def get_posts(cls, user, count=6):
#try:
token_response = requests.get(url=cls.token_url, params=cls.params)
access_token = token_response.text.split('=')[1]
graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)
query_string = 'posts?limit={0}'.format(count)
posts = graph.get_connections(profile['id'], query_string)
return posts
#except facebook.GraphAPIError:
# return None
if __name__ == "__main__":
posts = FacebookFeed.get_posts(user="Pepsi")
Code: Alles auswählen
django.core.exceptions.ImproperlyConfigured: Requested setting SOCIAL_AUTH_FACEBOOK_KEY, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Code: Alles auswählen
Traceback (most recent call last):
File "facebook.py", line 2, in <module>
import facebook
File "/home/marvin/facebook.py", line 5, in <module>
class FacebookFeed:
File "/home/marvin/facebook.py", line 8, in FacebookFeed
params = dict(client_id=settings.SOCIAL_AUTH_FACEBOOK_KEY, client_secret=settings.SOCIAL_AUTH_FACEBOOK_SECRET, grant_type='client_credentials')
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
return getattr(self._wrapped, name)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 159, in __getattr__
return getattr(self.default_settings, name)
AttributeError: 'module' object has no attribute 'SOCIAL_AUTH_FACEBOOK_KEY'