【问题标题】:Streaming tweets using Twython keeps breaking with chunkedencodingerror使用 Twython 的流式推文不断出现 chunkedencodingerror
【发布时间】:2014-08-28 23:00:24
【问题描述】:

我对编程比较陌生,但只是想通过使用 Twython twitter 包装器从 twitter 中提取推文来感受为 apis/data 工作的感觉。每次我这样做时,我都会收到大约 5,000 条推文的以下错误消息。我可以将流式传输与其他包装器(如 python-twitter)一起使用,并获得更远的约 800,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()

基本上,现在我什至没有尝试对推文做任何事情,因为我只是想看看我是否可以让它在没有错误的情况下工作。

【问题讨论】:

    标签: python twitter twython


    【解决方案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?

    【讨论】:

    • 根据 Twython Github 页面上的讨论,这可能表明您的进程落后并且 Twitter 正在重置队列。因此,尽管您的解决方案可行,但您可能会错过流中的一些推文。见github.com/ryanmcgrath/twython/issues/288
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多