简体   繁体   English

我想设置最大推文数的限制

[英]I want to set the limit of maximum number of Tweets

I am very new to python.我对python很陌生。 I'm using tweepy library to scrape tweets via twitter streaming API.我正在使用 tweepy 库通过 Twitter 流 API 抓取推文。 but it seems like connection gets broken after running for an hour.但似乎连接在运行一个小时后断开了。 I want to know if there is any way to stop the program from running before the connections get broken.我想知道是否有任何方法可以在连接中断之前阻止程序运行。 In short limiting the tweets.简而言之,限制推文。

I have tried the .items method but it did'nt work as it gives the name Error.我已经尝试过 .items 方法,但它没有工作,因为它给出了名称错误。

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


  ckey="xxxxxxxxxxxxxxxxxxxxxxxxxxx"
  csecret="xxxxxxxxxxxxxxxxxxxxxx"
  atoken="xxxxxxxxxxxxxxxxxxxxx"
  asecret="xxxxxxxxxxxxxxxxxxxxxxxxxxx"

  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=["Obama"])

thanks谢谢

To solve your connection issue take help from this:要解决您的连接问题,请从以下方面获得帮助:

Tweepy Connection broken: IncompleteRead - best way to handle exception? Tweepy 连接中断:IncompleteRead - 处理异常的最佳方法? or, can threading help avoid? 或者,线程可以帮助避免吗?

To achieve tweets limitation you can return False from the class def on_data method, when the desired number of tweets are fetched.要实现推文限制,您可以在获取所需数量的推文时从类def on_data方法return False Set max number of tweets in the init method and use try and except for error handling.init方法中设置最大推文数并使用try and except进行错误处理。 This might help这可能有帮助

def __init__(self):
    super().__init__()
    self.max_tweets = 10
    self.tweet_count = 0

def on_data(self, data):
    try:
     data
    except TypeError:
        print(completed)
    else:
     self.tweet_count+=1
     if(self.tweet_count==self.max_tweets):
       print("completed")
       return(False)
     else:
      decoded = json.loads(data)

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

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