Skip to content

Instantly share code, notes, and snippets.

@tndoan
Created January 2, 2014 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tndoan/8214409 to your computer and use it in GitHub Desktop.
Save tndoan/8214409 to your computer and use it in GitHub Desktop.
It is the example of using Python to request the result from Sentiment 140. This example is based on http://help.sentiment140.com/api
import urllib2
import json
s = '{"data": [{"text": "I love Titanic."},{"text": "I hate Titanic."}]}' # 2 short text that we want to do sentiment analysis
response = urllib2.urlopen('http://www.sentiment140.com/api/bulkClassifyJson', s) # request to server
page = response.read() # get the response
print page # print the result
json.loads(page) # parse the result. The result is in JSON format
@abhishek0318
Copy link

abhishek0318 commented Aug 31, 2018

For Python 3,

from urllib.request import urlopen
import json

s = '{"data": [{"text": "I love Titanic."},{"text": "I hate Titanic."}]}' # 2 short text that we want to do sentiment analysis

response = urlopen('http://www.sentiment140.com/api/bulkClassifyJson', data=bytes(json.dumps(s), encoding="utf-8")) # request to server
 
page = response.read() # get the response
 
print(page) # print the result
 
json.loads(page) # parse the result. The result is in JSON format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment