My aim is to perform at least 3 different types of sentiment analysis on data collected from twitter.
I'm attempting to perform sentiment analysis on tweets I've gathered using Python and Twitter API and stored in a database (MySQL & PhpMyAdmin). There are 3 problems I'm having:
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
Python
import urllib.request
import json
url = 'http://www.sentiment140.com/api/bulkClassifyJson'
values = {'data': [{'text': 'I love Titanic.'}, {'text': 'I hate Titanic.'}]}
data = json.dumps(values) # instead of urllib.urlencode(values)
response = urllib.request.urlopen(url, data)
page = response.read()
All help will be greatly appreciated, thank you!
As stated in the urllib docs , you have to convert your strs to bytes before feeding it to urlopen:
Note that params output from urlencode is encoded to bytes before it is sent to urlopen as data.
You can do so by calling .encode()
on data
:
import urllib.request
import json
url = 'http://www.sentiment140.com/api/bulkClassifyJson'
values = {'data': [{'text': 'I love Titanic.'}, {'text': 'I hate Titanic.'}]}
data = json.dumps(values)
response = urllib.request.urlopen(url, data=data.encode("utf-8"))
page = response.read()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.