【发布时间】:2015-09-19 15:05:41
【问题描述】:
我正在编写一个代码,该代码根据搜索词从 twitter 获取实时推文并将其保存到 Mysql 数据库。但是当我在插入数据库时运行代码时会引发错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 139: ordinal not in range(128)
我不明白这里的问题是要插入数据库的代码
tweet = json.loads(data);
#print json.dumps(tweet, indent=4, sort_keys=True)
#print tweet['text']
tweetid = tweet['id_str']
userid = tweet['user']['id_str']
text = tweet['text'].encode('utf-8')
cur.execute("""INSERT INTO twitterfeeeds(tweet_id, user_id,body,status) VALUES (%s,%s,%s,'0')"""%(tweetid,userid,text))
db.commit()
这里的正文是推文中的文本,状态是它是否被处理。
【问题讨论】:
标签: python mysql python-2.7 twitter