【发布时间】: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()
基本上,现在我什至没有尝试对推文做任何事情,因为我只是想看看我是否可以让它在没有错误的情况下工作。
【问题讨论】: