TweepError:期待长度,意外值发现pandas jupyter笔记本

时间:2017-04-16 22:01:57

标签: python pandas nlp tweepy

这是我的代码,试图初始化一个类的实例,我得到一个Tweep错误

query = 'vacation'
max_tweets = 1000

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

返回的错误如下所示:

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

我试图对此做一些研究,但我运气不好,非常感谢帮助!谢谢!

1 个答案:

答案 0 :(得分:0)

我正在使用tweepy库的克隆版本(因为开发团队没有将最新版本推送到pip,而之前的版本偶尔会抛出Nonetype错误)。我多次遇到同样的错误。错误在以下方法中:

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)

我用

raise TweepError('Expecting length, unexpected value found')
替换了

self.listener.keep_alive()
到目前为止它工作正常,但最终开发团队需要解决这个问题。