I am trying to use Python to request data from the Sentiment140 API.
The API is using a Bulk Classification Service (JSON). Within terminal it is working fine
curl -d "{'data': [{'text': 'I love Titanic.'}, {'text': 'I hate Titanic.'}]}" http://www.sentiment140.com/api/bulkClassifyJson
leading to the following response:
{"data":[{"text":"I love Titanic.","polarity":4,"meta":{"language":"en"}},{"text":"I hate Titanic.","polarity":0,"meta":{"language":"en"}}]}
I thought I could just use urllib to obtain the same response from my python code. I tried:
import urllibimport urllib2url = 'http://www.sentiment140.com/api/bulkClassifyJson'values = {'data': [{'text': 'I love Titanic.'}, {'text': 'I hate Titanic.'}]}data = urllib.urlencode(values)response = urllib2.urlopen(url, data)page = response.read()
The code works yet it does not donate me any results.
Am I missing something?
I think you need to use json here.
Try to do:
data = json.dumps(values) # instead of urllib.urlencode(values)response = urllib2.urlopen(url, data)page = response.read()
and on the top
import json
No comments:
Post a Comment