TypeError: cannot concatenate 'str' and 'int' objects when using tweepy

TypeError: cannot concatenate 'str' and 'int' objects when using tweepy

当我一遍又一遍地尝试 运行 编码时,我有时会随机得到一个错误

File "C:\Users\xxxx\Desktop\Startup\twitterapi.py", line 54, in <module>
twitterStream.filter(track=["asdfqw"])
File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 430, in filter
self._start(async)
File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 346, in _start
 self._run()
File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 286, in _run
raise exception
TypeError: cannot concatenate 'str' and 'int' objects

有时代码可以正常工作,有时则不能。当我重新 运行 几次时,它最终起作用了。可能是什么问题?我的代码或 tweepy 有问题吗? 这是我的代码

class listener(StreamListener):
def on_data(self, data):
    tweet = json.loads(data);
    #print json.dumps(tweet, indent=4, sort_keys=True)
    #print tweet['text']
    try:
        tweetid = tweet['id_str']
        print tweetid
    except:
        try:
            tweetid = tweet['id']
        except:
            pass
    try:
        userid = tweet['user']['id_str']
    except:
        pass
    try:
        text = tweet['text']
    except:
        pass
    cur.execute(r"INSERT INTO twitterfeeds(tweet_id, user_id,body,status) VALUES (%s, %s, %s, '0')",(tweetid, userid, text))
    db.commit()
    #print "pass"
    return True
def on_error(self, status):
    print "Error: " + status
#while True:
#   try:

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["asdfqw"])
#    except:
#       pass

替换:

print "Error: " + status

print "Error: " + str(status)