使用Twython的流媒体推文不断打破chunkedencodingerror

时间:2014-08-27 01:49:45

标签: python twitter twython

我对编程比较陌生,但只是想通过使用Twython twitter包装器从推特中提取推文来尝试apis /数据。每次我这样做,我得到约5,000条推文的以下错误消息。我可以使用与其他包装器一起使用流式传输,例如python-twitter,并且在没有类似错误的情况下获得更多~808,000条推文。

--------------------------------------------------------------------------
ChunkedEncodingError                      Traceback (most recent call last)
<ipython-input-5-fdcf34f23648> in <module>()
      1 #[stream.statuses.filter(track='twitter')]
----> 2 stream.statuses.sample()

/Users/myusername/anaconda/lib/python2.7/site-packages/twython/streaming/types.pyc in sample(self, **params)
     75         url = 'https://stream.twitter.com/%s/statuses/sample.json' \
     76               % self.streamer.api_version
---> 77         self.streamer._request(url, params=params)
     78 
     79     def firehose(self, **params):

/Users/myusername/anaconda/lib/python2.7/site-packages/twython/streaming/api.pyc in _request(self, url, method, params)
    132             response = _send(retry_counter)
    133 
--> 134             for line in response.iter_lines(self.chunk_size):
    135                 if not self.connected:
    136                     break

/Users/myusername/anaconda/lib/python2.7/site-packages/requests/models.pyc in iter_lines(self, chunk_size, decode_unicode)
    643 
    644         for chunk in self.iter_content(chunk_size=chunk_size,
--> 645                                        decode_unicode=decode_unicode):
    646 
    647             if pending is not None:

/Users/myusername/anaconda/lib/python2.7/site-packages/requests/models.pyc in generate()
    616                         yield chunk
    617                 except IncompleteRead as e:
--> 618                     raise ChunkedEncodingError(e)
    619             except AttributeError:
    620                 # Standard file-like object.

ChunkedEncodingError: IncompleteRead(0 bytes read, 1 more expected)

我用来生成此代码的代码如下。我确信它缺少很多,包括处理这个特殊异常的方法。

from twython import TwythonStreamer
import numpy as np
import pandas as pd
from requests.exceptions import ChunkedEncodingError

counter = 0
class MyStreamer(TwythonStreamer):
    def on_success(self, data):
        global counter
        #if 'text' in data:
            #print data['text'].encode('utf-8')
        counter+=1
        print counter

    def on_error(self, status_code, data):
        print status_code

        # Want to stop trying to get data because of the error?
        # Uncomment the next line!
        # self.disconnect()

stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.sample()

基本上,现在我甚至都没有尝试用推文做点什么,因为我只是想知道我是否可以在没有错误的情况下使用它。

1 个答案:

答案 0 :(得分:1)

好吧,我没有弄清楚导致错误的是什么,但是如果我改变了最后一行代码就行了。像这样:

def streamed():
    while True:
        try:
            stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
            stream.statuses.filter(track='twitter', stall_warnings=True)
        except  ChunkedEncodingError:
            continue

streamed()

当错误发生时,这会让流媒体再次出现。 我还必须添加

return True

到mystreamer类中的函数。

此答案也有助于之前:How to restart tweepy script in case of error?