简体   繁体   English

ReadTimeoutError:Twitter流API

[英]ReadTimeoutError: Twitter Streaming API

I want to get some tweets regarding aggressive dogs. 我想获得一些关于好斗的狗的推文。 My keywords are specified in the code. 我的关键字在代码中指定。 All of them refer to German shepherd (In Spanish "pastor alemán"). 他们全都指德国牧羊犬(西班牙语为“ pastoralemán”)。 For instance, among other tweets I expect to get this one that perfectly fits the keywords and was posted on 23 Feb 2015. I executed the below-given code and after around 1 hour of waiting the following error appeared: 例如,在其他推文中,我希望得到一条完全适合关键字的推文,并于2015年2月23日发布。我执行了下面给出的代码,等待了大约1小时后出现以下错误:

requests.packages.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='stream.twitter.com', port=443): Read timed out.

It seems that there is some problem with the port 443. How to solve this problem? 看来443端口有问题。如何解决此问题?

PS The code works fine with keywords like "python, javascript". PS该代码可以与“ python,javascript”之类的关键字配合使用。

UPDATE: I noticed that the code retrieves some tweets if I write keywords in English, like "German shepherd aggressive". 更新:我注意到,如果我用英语编写关键字,例如“德国牧羊犬进取”,代码会检索一些推文。 But then I receive another error message: 但随后我又收到一条错误消息:

socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host

My code: 我的代码:

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

CONSUMER_KEY = "..."
CONSUMER_SECRET = "..."
ACCESS_TOKEN = "..."
ACCESS_TOKEN_SECRET = "..."

class listener(StreamListener):

    def on_data(self, data):
        try:
            print data
            saveFile = open('raw_tweets.json', 'a')
            saveFile.write(data)
            saveFile.write('\n')
            saveFile.close()
            return True

        except BaseException, e:
            print 'failed ondata,', str(e)
            time.sleep(10)
            pass

    def on_error(self, status):
        print status
        if status == 420:
            return False


if __name__ == '__main__':
    auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    stream = Stream(auth, listener())

    keywords = ['pastor aleman agresivo','pastor aleman muerde',
                'pastor aleman mata','pastor aleman muerte',
                'pastor aleman peligroso','pastor aleman peligro',
                'pastor aleman adiestramiento']
    stream.filter(track=keywords)

Catch these errors and restart the stream. 捕获这些错误并重新启动流。 The errors are normal. 错误是正常的。 Connections may break for a number of reasons you have no control over. 由于您无法控制的多种原因,连接可能会中断。 Also, Twitter will close the connection if there is no activity after 90 seconds. 同样,如果90秒钟后没有任何活动,Twitter将关闭连接。

EDIT: Someone posted an example using tweepy that does something similar to what you need. 编辑:有人发布了一个使用tweepy的示例 ,该示例执行的操作类似于您需要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM