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
Question
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 get the same response from my python code. I tried:
import urllib
import urllib2
url = '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 but it does not give me any results. Am I missing something?
Solution
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