我正在尝试从本教程tulane.edu-twitter运行以下脚本,我收到以下错误:
File "GetTextFromTwitter.py", line 61, in <module>
stream.filter(track=['de'], languages=['es'])
File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 428, in filter
self._start(async)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 346, in _start
self._run()
File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 239, in _run
verify=self.verify)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
timeout=timeout
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 349, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 995, in request
self._send_request(method, url, body, headers)
File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 1029, in _send_request
self.endheaders(body)
File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 991, in endheaders
self._send_output(message_body)
File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 848, in _send_output
self.send(message_body)
File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 820, in send
self.sock.sendall(data)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 208, in sendall
sent = self._send_until_done(data)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 198, in _send_until_done
return self.connection.send(data)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/OpenSSL/SSL.py", line 947, in send
raise TypeError("data must be a byte string")
TypeError: data must be a byte string
我还尝试按照youtube上的教程进行操作,我遇到了同样的错误:
File "tweepyTest.py", line 39, in <module>
twitterStream.filter(track=["car"])
File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 428, in filter
self._start(async)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 346, in _start
self._run()
File "/Users/enricok/anaconda/lib/python2.7/site-packages/tweepy/streaming.py", line 239, in _run
verify=self.verify)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
timeout=timeout
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 349, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 995, in request
self._send_request(method, url, body, headers)
File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 1029, in _send_request
self.endheaders(body)
File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 991, in endheaders
self._send_output(message_body)
File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 848, in _send_output
self.send(message_body)
File "/Users/enricok/anaconda/lib/python2.7/httplib.py", line 820, in send
self.sock.sendall(data)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 208, in sendall
sent = self._send_until_done(data)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 198, in _send_until_done
return self.connection.send(data)
File "/Users/enricok/anaconda/lib/python2.7/site-packages/OpenSSL/SSL.py", line 947, in send
raise TypeError("data must be a byte string")
TypeError: data must be a byte string
几天前它完美运行(仍然有.csv它返回)。但是现在和我尝试过的其他脚本一样,使用.filter
方法时会出现多个错误。如果我将.filter()
替换为.sample()
,则可行。我唯一记得的就是做一个可能破坏某事的brew update
。
这里是第一个提到的教程试图过滤西班牙语推文的代码。
任何想法如何解决这个问题?
import tweepy
import requests
from tweepy import API
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
API_KEY = ''
API_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''
key = tweepy.OAuthHandler(API_KEY, API_SECRET)
key.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(key)
class Stream2Screen(tweepy.StreamListener):
def __init__(self, api=None):
self.api = api or API()
self.n = 0
self.m = 20
def on_status(self, status):
print status.text.encode('utf8')
self.n = self.n+1
if self.n < self.m:
return True
else:
print 'tweets = '+str(self.n)
return False
stream = tweepy.streaming.Stream(key, Stream2Screen())
stream.filter(track=['de'], languages=['es'])
# stream.sample()