fleaheap
fleaheap

Reputation: 166

Facebook Graph API encoding - Python

I am going around in circles and tried so many different ways so I guess my core understanding is wrong. I would be grateful for help in understanding my encoding/decoding issues.

import urllib2

result = urllib2.urlopen("https://graph.facebook.com/163146530455639")
rawdata = result.read().decode('utf-8')

print "HEADER: " + str(result.info())

print "I want this to work ", rawdata.find('http://www.facebook.com')
print "I dont want this to work ", rawdata.find('http:\/\/www.facebook.com')

I guess what im getting isnt utf-8 even though the header seems to say it is. Or as a newbie to Python im doing something dumb. :(

Thanks for any help,

Phil

Upvotes: 0

Views: 992

Answers (1)

richsilv
richsilv

Reputation: 8013

You're getting JSON back from Facebook, so the easiest thing to do is use the built in json module to decode it (provided you're using Python 2.6+, otherwise you'll have to install).

import json
import urllib2

result = urllib2.urlopen("https://graph.facebook.com/163146530455639")
rawdata = result.read()
jsondata = json.load(rawdata)

print jsondata['link']

gives you:

u'http://www.facebook.com/GrosvenorCafe'

Upvotes: 1

Related Questions