简体   繁体   中英

Posting data to Sentiment140 for Sentiment Analysis

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:

  1. There aren't many API's that perform sentiment analysis (any suggestions would be helpful)
  2. I'm attempting to connect to the Sentiment140 API but my code is returning the following error

TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.

  1. Is there a general way to connect the Sentiment140 API (or any other sentiment analysis API) with my MySQL database for analysis of the text column in the database?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM