gpt4 book ai didi

python - 通过 Streaming API 下载 Twitter 数据时出现错误

转载 作者:太空宇宙 更新时间:2023-11-03 17:27:35 26 4
gpt4 key购买 nike

我正在使用 Python 2.7 和 tweepy 下载 Twitter 数据。但大约 1 MB 后,下载停止。

我的代码如下:

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener


#consumer key, consumer secret, access token, access secret.
ckey="NlilpJMYf7i4dGYXtJJONrDt5"
csecret="S1xIaTJs2M1S4Okfm3LF1JkjGkNBOaDpkNl6HayOPcLFLWMrak"
atoken="1144571546-MnpLF9GQqVdo5QzBysL1iZFW0p49kixER1xkHvK"
asecret="LnVOgnFlgIaVd0qBYf7bJ9Dzl0nE2oKrjBDYc0L69XEsH"



class listener(StreamListener):

def on_data(self, data):
print(data)
return(True)

def on_error(self, status):
print status

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

twitterStream = Stream(auth, listener())
twitterStream.filter(track= ['nepal','police','army','constitution','earthquake','government','minister','kathmandu post','janakpur','everest'])

Screenshot of the Traceback我使用命令 python tweet.py > twitter_data.text

运行它

下载大约 1.4 MB 后得到的回溯屏幕截图是:

最佳答案

由于某种原因,某处出现连接问题。您可能想以某种方式处理它。例如,通过重新连接来处理 ProtocolError

from requests.packages.urllib3.exceptions import ProtocolError
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener


CKEY = 'NlilpJMYf7i4dGYXtJJONrDt5'
CSECRET = 'S1xIaTJs2M1S4Okfm3LF1JkjGkNBOaDpkNl6HayOPcLFLWMrak'
ATOKEN = '1144571546-MnpLF9GQqVdo5QzBysL1iZFW0p49kixER1xkHvK'
ASECRET = 'LnVOgnFlgIaVd0qBYf7bJ9Dzl0nE2oKrjBDYc0L69XEsH'


class Listener(StreamListener):

@staticmethod
def on_data(data):
print(data)
return True

@staticmethod
def on_error(status):
print(status)


def main():
auth_handler = OAuthHandler(CKEY, CSECRET)
auth_handler.set_access_token(ATOKEN, ASECRET)

while True:
try:
stream = Stream(auth_handler, Listener())
stream.filter(
track=[
'nepal', 'police', 'army', 'constitution', 'earthquake',
'government', 'minister', 'kathmandu post', 'janakpur',
'everest',
]
)
except ProtocolError as error:
print(error)


if __name__ == '__main__':
main()

关于python - 通过 Streaming API 下载 Twitter 数据时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32350824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com