python - 类型错误 : 'str' object is not callable when insert tweet data to mysql Python 3

标签 python mysql tweepy pymysql

这是我在 MYSQL 中插入推文数据的代码

import pymysql
import tweepy
import time
import json
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import pymysql.cursors

ckey= ''
csecret= ''
atoken=''
asecret=''

conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='admin1234', db='mysql')
cur = conn.cursor()

class listener(StreamListener):

 def on_data(self, data):
        all_data = json.loads(data)
        tweet = all_data["text"]
        a=0
        #username = all_data["user"]["screen_name"]

        cur.execute("INSERT INTO tweet (textt) VALUES (%s)" (tweet))
        print (tweet)
        return True

def on_error(self, status):
    print (status)

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track = ["puasa"])

cur.close()
conn.close()

但我收到错误 TypeError: 'str' object is not callable

回溯错误

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

最佳答案

您需要 2 个额外的逗号:

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

第一个将查询字符串与参数分开,第二个将括号中的值转换为 1 元素元组中的第一个元素(如果您只使用单个字符串而不是元组,则实际上是 would work ,假设您只使用有一个论点,但从表面上看这并没有得到官方支持)。

但是您在评论中提到的这个错误:

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

表示您正在尝试将包含扩展字符集中的字符的 unicode 文本解释为 latin-1

如果该字段已在内部定义(在您的 mysql 数据库中)为 unicode,您可能需要指定连接时使用的字符集,例如:

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

如果 mysql 中的字段还不是 utf-8 之类的东西,那么我建议您更改或以其他方式重新定义数据库以为此列使用 unicode 字符集。

https://dev.mysql.com/doc/refman/8.0/en/charset-mysql.html

关于python - 类型错误 : 'str' object is not callable when insert tweet data to mysql Python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50492652/

相关文章:

python - 如何在Python中的一列中显示不同位数的小数

python - 有没有办法用 psycopg2 询问用户是否输入密码?

java - 回滚不起作用

mysql - UFW 3306 端口转发在 RaspberryPi 2 上不起作用

python - 带有 Tweepy 的 Twitter Streaming API 拒绝 oauth

twitter - Tweepy 错误 104 : Connection aborted

python - 如何在 python + webapp2 中处理多选表单字段?

python - 如何在Wireshark中解析protobuf数据包

mysql join 2表选择一行字段有多行

python - StremListener 在 tweepy 中不返回 JSON 数据