让我的 Tweepy 流停止时遇到问题
Having issues getting my Tweepy stream to stop
当我达到 100 条推文后无法让我的流退出。我试过很多方法。希望保留 "with open."
的用法
*在文件打开时使用 while 循环会导致乱七八糟的不需要的 JSON 文件
*当前断开连接的使用在达到 100 后继续流式传输但数据已损坏
已编辑:
- 使用 self 重新创建了推文数量和推文总数。
- 每次加载数据时,num_tweets 都会更新:self.num_tweets+=1。然后更新pbar:self.pbar.update(1)
- 在 try 语句之外,如果 self.num_tweets < self.total_tweets,则 return True,否则 self.pbar.close() 和 return False
来源: 克里斯·库克曼
def __init__(self, api=None):
self.num_tweets = 0
self.total_tweets = int(input("Number of tweets:"))
self.pbar = tqdm(total=self.total_tweets)
self.unsaved = 0
self.emojis = 0
def on_data(self, data):
try:
portal_1 = creds()
rawTweets = json.loads(data)
self.num_tweets += 1
self.pbar.update(1)
...
...
except BaseException as e:
print(colored("Error on_data: %s", "red") % str(e))
if self.num_tweets < self.total_tweets:
return True
else:
self.pbar.close()
return False
要使用 tweepy 退出流,您需要从 on_status 函数中设置 return false,因此如果您更改:
if self.num_tweets < 100:
return True
else:
twitter_stream.disconnect()
收件人:
if self.num_tweets < 100:
return True
else:
self.pbar.close() # Closes the instance of the progress bar.
return False # Closes the stream.
这应该可以解决问题。另外,每次 运行:
时添加 self.num_tweets 作为进度条
self.pbar.update(self.num_tweets)
随着更新,您每次添加推文的数量:
Tweet 1 | Tweet Count ='s 1 | Progress bar ='s 1 (1)
Tweet 2 | Tweet Count ='s 2 | Progress bar ='s 1 + 2 (3)
Tweet 3 | Tweet Count ='s 3 | Progress bar ='s 3 + 3 (6)
根据你的代码,我假设你的意图是增加它们,所以要做到这一点,你只需要将它更改为:
self.pbar.update(1)
希望这对您有所帮助。
当我达到 100 条推文后无法让我的流退出。我试过很多方法。希望保留 "with open."
的用法*在文件打开时使用 while 循环会导致乱七八糟的不需要的 JSON 文件 *当前断开连接的使用在达到 100 后继续流式传输但数据已损坏
已编辑:
- 使用 self 重新创建了推文数量和推文总数。
- 每次加载数据时,num_tweets 都会更新:self.num_tweets+=1。然后更新pbar:self.pbar.update(1)
- 在 try 语句之外,如果 self.num_tweets < self.total_tweets,则 return True,否则 self.pbar.close() 和 return False
来源: 克里斯·库克曼
def __init__(self, api=None):
self.num_tweets = 0
self.total_tweets = int(input("Number of tweets:"))
self.pbar = tqdm(total=self.total_tweets)
self.unsaved = 0
self.emojis = 0
def on_data(self, data):
try:
portal_1 = creds()
rawTweets = json.loads(data)
self.num_tweets += 1
self.pbar.update(1)
...
...
except BaseException as e:
print(colored("Error on_data: %s", "red") % str(e))
if self.num_tweets < self.total_tweets:
return True
else:
self.pbar.close()
return False
要使用 tweepy 退出流,您需要从 on_status 函数中设置 return false,因此如果您更改:
if self.num_tweets < 100:
return True
else:
twitter_stream.disconnect()
收件人:
if self.num_tweets < 100:
return True
else:
self.pbar.close() # Closes the instance of the progress bar.
return False # Closes the stream.
这应该可以解决问题。另外,每次 运行:
时添加 self.num_tweets 作为进度条self.pbar.update(self.num_tweets)
随着更新,您每次添加推文的数量:
Tweet 1 | Tweet Count ='s 1 | Progress bar ='s 1 (1)
Tweet 2 | Tweet Count ='s 2 | Progress bar ='s 1 + 2 (3)
Tweet 3 | Tweet Count ='s 3 | Progress bar ='s 3 + 3 (6)
根据你的代码,我假设你的意图是增加它们,所以要做到这一点,你只需要将它更改为:
self.pbar.update(1)
希望这对您有所帮助。