TweepError: Expecting length, unexpected value found pandas jupyter notebook

我的未来我决定 提交于 2019-12-08 14:03:00

问题


This is my code, trying to initialize an instance of a class and I am getting a Tweep Error

query = 'vacation'
max_tweets = 1000

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener = myStreamListener)
myStream.filter(track=[query])

the error that is returned looks like this:

TweepError                                Traceback (most recent call last)
<ipython-input-31-227d7853c42e> in <module>()
      4 myStreamListener = MyStreamListener()
      5 myStream = tweepy.Stream(auth = api.auth, listener = myStreamListener)
----> 6 myStream.filter(track=[query])

C:\Program Files\Anaconda3\lib\site-packages\tweepy-3.6.0-py3.5.egg\tweepy\streaming.py in filter(self, follow, track, async, locations, stall_warnings, languages, encoding, filter_level)
    448         self.session.params = {'delimited': 'length'}
    449         self.host = 'stream.twitter.com'
--> 450         self._start(async)
    451 
    452     def sitestream(self, follow, stall_warnings=False,

C:\Program Files\Anaconda3\lib\site-packages\tweepy-3.6.0-py3.5.egg\tweepy\streaming.py in _start(self, async)
    362             self._thread.start()
    363         else:
--> 364             self._run()
    365 
    366     def on_closed(self, resp):

C:\Program Files\Anaconda3\lib\site-packages\tweepy-3.6.0-py3.5.egg\tweepy\streaming.py in _run(self)
    295             # call a handler first so that the exception can be logged.
    296             self.listener.on_exception(exc_info[1])
--> 297             six.reraise(*exc_info)
    298 
    299     def _data(self, data):

C:\Program Files\Anaconda3\lib\site-packages\six.py in reraise(tp, value, tb)
    684         if value.__traceback__ is not tb:
    685             raise value.with_traceback(tb)
--> 686         raise value
    687 
    688 else:

C:\Program Files\Anaconda3\lib\site-packages\tweepy-3.6.0-py3.5.egg\tweepy\streaming.py in _run(self)
    264                     self.snooze_time = self.snooze_time_step
    265                     self.listener.on_connect()
--> 266                     self._read_loop(resp)
    267             except (Timeout, ssl.SSLError) as exc:
    268                 # This is still necessary, as a SSLError can actually be

C:\Program Files\Anaconda3\lib\site-packages\tweepy-3.6.0-py3.5.egg\tweepy\streaming.py in _read_loop(self, resp)
    321                     break
    322                 else:
--> 323                     raise TweepError('Expecting length, unexpected value found')
    324 
    325             next_status_obj = buf.read_len(length)

TweepError: Expecting length, unexpected value found

I tried to do some research on this, but I ran out of luck, and help is very much appreciated! thank you!


回答1:


I am working with the cloned version of tweepy library (as the dev team have not pushed the latest release to pip and the previous version throws a Nonetype error now and then). I ran into the same error a number of times. The error is in the following method:

def _read_loop(self, resp):
    charset = resp.headers.get('content-type', default='')
    enc_search = re.search('charset=(?P<enc>\S*)', charset)
    if enc_search is not None:
        encoding = enc_search.group('enc')
    else:
        encoding = 'utf-8'

    buf = ReadBuffer(resp.raw, self.chunk_size, encoding=encoding)

    while self.running and not resp.raw.closed:
        length = 0
        while not resp.raw.closed:
            line = buf.read_line()
            if not line:
                self.listener.keep_alive()  # keep-alive new lines are expected
            elif line.strip().isdigit():
                length = int(line)
                break
            else:
                raise TweepError('Expecting length, unexpected value found')

        next_status_obj = buf.read_len(length)
        if self.running and next_status_obj:


self._data(next_status_obj)

I have replaced

raise TweepError('Expecting length, unexpected value found')
with
self.listener.keep_alive()
And so far it is working ok but ultimately the tweepy dev team needs to address this.

来源:https://stackoverflow.com/questions/43442616/tweeperror-expecting-length-unexpected-value-found-pandas-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!