RuntimeError: No active exception to reraise with tweepy
RuntimeError: No active exception to reraise with tweepy
我正在使用 python 代码进行基于 Twitter 的实时情绪分析。
它应该可以工作,因为 youtube 上也有一个教程,但在我的电脑上,这是我遇到的错误:
File "<ipython-input-2-9dc468222105>", line 1, in <module>
runfile('C:/Users/marco/Anaconda3/envs/coinlive/social_functions.py',
wdir='C:/Users/marco/Anaconda3/envs/coinlive')
File "C:\Users\marco\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 710, in runfile
execfile(filename, namespace)
File "C:\Users\marco\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/marco/Anaconda3/envs/coinlive/social_functions.py", line 82,
in <module>
twitterStream.filter(track=["Donald Trump"])
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
450, in filter
self._start(async)
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
364, in _start
self._run()
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
297, in _run
six.reraise(*exc_info)
File "C:\Users\marco\Anaconda3\lib\site-packages\six.py", line 693, in reraise
raise value
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
266, in _run
self._read_loop(resp)
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
327, in _read_loop
self._data(next_status_obj)
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
300, in _data
if self.listener.on_data(data) is False:
File "C:/Users/marco/Anaconda3/envs/coinlive/social_functions.py", line 39,
in on_data
tweet=" ".join(re.findall("[a-zA-Z]+", tweet))
File "C:\Users\marco\Anaconda3\lib\re.py", line 222, in findall
return _compile(pattern, flags).findall(string)
TypeError: cannot use a string pattern on a bytes-like object
我认为错误与 tweepy 库有关,我阅读了很多帖子,但没有任何好的结果。
这是我的代码:
import time
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
from textblob import TextBlob
import matplotlib.pyplot as plt
import re
"# -- coding: utf-8 --"
def calctime(a):
return time.time()-a
positive=0
negative=0
compound=0
count=0
initime=time.time()
plt.ion()
ckey='R8adMtTxKPXseiFqFcb7XBGJv'
csecret='rkcxNsg8Q09AiVDgh4bn5GXNpsLP0jLwekqIOkrdkwa1K1h9oc'
atoken='232118221-5SPjlFvC22JBRXODNdNWoEDJwvpvaiKoXAazpAHH'
asecret='rzZ1NMxfgK6IYzTuEI0rMvpK04lJj49tiKe1BaST9bmcT'
class listener(StreamListener):
def on_data(self,data):
global initime
t=int(calctime(initime))
all_data=json.loads(data)
tweet=all_data["text"].encode("utf-8")
#username=all_data["user"]["screen_name"]
tweet=" ".join(re.findall("[a-zA-Z]+", tweet))
blob=TextBlob(tweet.strip())
global positive
global negative
global compound
global count
count=count+1
senti=0
for sen in blob.sentences:
senti=senti+sen.sentiment.polarity
if sen.sentiment.polarity >= 0:
positive=positive+sen.sentiment.polarity
else:
negative=negative+sen.sentiment.polarity
compound=compound+senti
print(count)
print(tweet.strip())
print(senti)
print(t)
print(str(positive) + ' ' + str(negative) + ' ' + str(compound))
plt.axis([ 0, 70, -20,20])
plt.xlabel('Time')
plt.ylabel('Sentiment')
plt.plot([t],[positive],'go',[t] ,[negative],'ro',[t],[compound],'bo')
plt.show()
plt.pause(0.0001)
if count==200:
return False
else:
return True
def on_error(self,status):
print(status)
auth=OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
twitterStream= Stream(auth, listener(count))
twitterStream.filter(track=["Donald Trump"])
您正在对字符串进行编码
tweet=all_data["text"].encode("utf-8")
然后尝试运行 re.findall就可以了:
tweet=" ".join(re.findall("[a-zA-Z]+", tweet))
尝试不调用编码
我正在使用 python 代码进行基于 Twitter 的实时情绪分析。 它应该可以工作,因为 youtube 上也有一个教程,但在我的电脑上,这是我遇到的错误:
File "<ipython-input-2-9dc468222105>", line 1, in <module>
runfile('C:/Users/marco/Anaconda3/envs/coinlive/social_functions.py',
wdir='C:/Users/marco/Anaconda3/envs/coinlive')
File "C:\Users\marco\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 710, in runfile
execfile(filename, namespace)
File "C:\Users\marco\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/marco/Anaconda3/envs/coinlive/social_functions.py", line 82,
in <module>
twitterStream.filter(track=["Donald Trump"])
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
450, in filter
self._start(async)
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
364, in _start
self._run()
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
297, in _run
six.reraise(*exc_info)
File "C:\Users\marco\Anaconda3\lib\site-packages\six.py", line 693, in reraise
raise value
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
266, in _run
self._read_loop(resp)
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
327, in _read_loop
self._data(next_status_obj)
File "C:\Users\marco\Anaconda3\lib\site-packages\tweepy\streaming.py", line
300, in _data
if self.listener.on_data(data) is False:
File "C:/Users/marco/Anaconda3/envs/coinlive/social_functions.py", line 39,
in on_data
tweet=" ".join(re.findall("[a-zA-Z]+", tweet))
File "C:\Users\marco\Anaconda3\lib\re.py", line 222, in findall
return _compile(pattern, flags).findall(string)
TypeError: cannot use a string pattern on a bytes-like object
我认为错误与 tweepy 库有关,我阅读了很多帖子,但没有任何好的结果。
这是我的代码:
import time
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
from textblob import TextBlob
import matplotlib.pyplot as plt
import re
"# -- coding: utf-8 --"
def calctime(a):
return time.time()-a
positive=0
negative=0
compound=0
count=0
initime=time.time()
plt.ion()
ckey='R8adMtTxKPXseiFqFcb7XBGJv'
csecret='rkcxNsg8Q09AiVDgh4bn5GXNpsLP0jLwekqIOkrdkwa1K1h9oc'
atoken='232118221-5SPjlFvC22JBRXODNdNWoEDJwvpvaiKoXAazpAHH'
asecret='rzZ1NMxfgK6IYzTuEI0rMvpK04lJj49tiKe1BaST9bmcT'
class listener(StreamListener):
def on_data(self,data):
global initime
t=int(calctime(initime))
all_data=json.loads(data)
tweet=all_data["text"].encode("utf-8")
#username=all_data["user"]["screen_name"]
tweet=" ".join(re.findall("[a-zA-Z]+", tweet))
blob=TextBlob(tweet.strip())
global positive
global negative
global compound
global count
count=count+1
senti=0
for sen in blob.sentences:
senti=senti+sen.sentiment.polarity
if sen.sentiment.polarity >= 0:
positive=positive+sen.sentiment.polarity
else:
negative=negative+sen.sentiment.polarity
compound=compound+senti
print(count)
print(tweet.strip())
print(senti)
print(t)
print(str(positive) + ' ' + str(negative) + ' ' + str(compound))
plt.axis([ 0, 70, -20,20])
plt.xlabel('Time')
plt.ylabel('Sentiment')
plt.plot([t],[positive],'go',[t] ,[negative],'ro',[t],[compound],'bo')
plt.show()
plt.pause(0.0001)
if count==200:
return False
else:
return True
def on_error(self,status):
print(status)
auth=OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
twitterStream= Stream(auth, listener(count))
twitterStream.filter(track=["Donald Trump"])
您正在对字符串进行编码
tweet=all_data["text"].encode("utf-8")
然后尝试运行 re.findall就可以了:
tweet=" ".join(re.findall("[a-zA-Z]+", tweet))
尝试不调用编码