typeerror:在mysql python 3中插入tweet数据时,无法调用“str”对象

xj3cbfub  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(453)

这是我在mysql中插入tweet数据的代码

  1. import pymysql
  2. import tweepy
  3. import time
  4. import json
  5. from tweepy import Stream
  6. from tweepy import OAuthHandler
  7. from tweepy.streaming import StreamListener
  8. import pymysql.cursors
  9. ckey= ''
  10. csecret= ''
  11. atoken=''
  12. asecret=''
  13. conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='admin1234', db='mysql')
  14. cur = conn.cursor()
  15. class listener(StreamListener):
  16. def on_data(self, data):
  17. all_data = json.loads(data)
  18. tweet = all_data["text"]
  19. a=0
  20. #username = all_data["user"]["screen_name"]
  21. cur.execute("INSERT INTO tweet (textt) VALUES (%s)" (tweet))
  22. print (tweet)
  23. return True
  24. def on_error(self, status):
  25. print (status)
  26. auth = OAuthHandler(ckey, csecret)
  27. auth.set_access_token(atoken, asecret)
  28. twitterStream = Stream(auth, listener())
  29. twitterStream.filter(track = ["puasa"])
  30. cur.close()
  31. conn.close()

但我犯了个错误 TypeError: 'str' object is not callable 回溯错误

  1. Traceback (most recent call last):
  2. File "collect-sql.py", line 40, in <module>
  3. twitterStream.filter(track = ["puasa"])
  4. File "/Users/amzar/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 450, in filter
  5. self._start(async)
  6. File "/Users/amzar/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 364, in _start
  7. self._run()
  8. File "/Users/amzar/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 297, in _run
  9. six.reraise(*exc_info)
  10. File "/Users/amzar/anaconda3/lib/python3.6/site-packages/six.py", line 693, in reraise
  11. raise value
  12. File "/Users/amzar/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 266, in _run
  13. self._read_loop(resp)
  14. File "/Users/amzar/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 327, in _read_loop
  15. self._data(next_status_obj)
  16. File "/Users/amzar/anaconda3/lib/python3.6/site-packages/tweepy/streaming.py", line 300, in _data
  17. if self.listener.on_data(data) is False:
  18. File "collect-sql.py", line 30, in on_data
  19. cur.execute("INSERT INTO tweet (textt) VALUES (%s)" (tweet))
  20. TypeError: 'str' object is not callable
sxpgvts3

sxpgvts31#

您需要另外两个逗号:

  1. cur.execute("INSERT INTO tweet (textt) VALUES (%s)", (tweet,))

第一种方法将查询字符串与参数分开,第二种方法将方括号中的值转换为1元素元组中的第一个元素(如果只使用单个字符串而不是元组,假设只有一个参数,这实际上是可行的,但从外观上看,这并不是官方支持的)。
但是你在评论中提到的这个错误:

  1. UnicodeEncodeError: 'latin-1' codec can't encode character '\u201c' in position 97: ordinal not in range(256)

表示您正试图将包含扩展字符集中字符的unicode文本解释为 latin-1 .
如果该字段已在内部定义为unicode(在mysql数据库中),则可能需要指定连接时使用的字符集,例如:

  1. conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='admin1234', db='mysql', use_unicode=True, charset="utf8")

如果mysql中的字段还不是utf-8,那么我建议您更改或重新定义数据库,以便在此列中使用unicode字符se tf。
https://dev.mysql.com/doc/refman/8.0/en/charset-mysql.html

展开查看全部

相关问题