[英]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. 我的目标是对从Twitter收集的数据进行至少3种不同类型的情绪分析。
I'm attempting to perform sentiment analysis on tweets I've gathered using Python and Twitter API and stored in a database (MySQL & PhpMyAdmin). 我正在尝试使用Python和Twitter API收集并存储在数据库(MySQL&PhpMyAdmin)中的推文上进行情感分析。 There are 3 problems I'm having:
我遇到3个问题:
TypeError: POST data should be bytes, an iterable of bytes, or a file object.
TypeError:POST数据应为字节,字节可迭代或文件对象。 It cannot be of type str.
它不能是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: 如urllib docs中所述,您必须先将strs转换为字节,然后再将其提供给urlopen:
Note that params output from urlencode is encoded to bytes before it is sent to urlopen as data.
请注意,在将urlencode输出的参数作为数据发送到urlopen之前,已将其编码为字节。
You can do so by calling .encode()
on data
: 您可以通过对
data
调用.encode()
来实现:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.