下面是我从上一个问题中摘录的代码。这段python代码的目的是为Twitter创建一个爬虫,我计划每小时运行一次代码,持续60秒。我将在7天的24小时内做这件事,看看推文中的趋势。
当前,此代码一直在运行,直到手动结束
import time
import threading
class listener(StreamListener):
def on_data(self, data):
try:
print data
saveFile = open('twitDB.csv','a')
saveFile.write(data)
saveFile.write('\n')
saveFile.close()
return True
except BaseException, e:
print 'failed ondata,' ,str(e)
time.sleep(5)
def on_error(self, status):
print status
time.sleep(5) 发布于 2013-12-27 00:09:17
将以下代码放入正在运行的线程中,而不是执行主代码的线程中,并尝试一下:
import sys,time
now=time.time()
while True:
end=time.time()
total=round(end-now)
if total==5:
sys.exit(0)https://stackoverflow.com/questions/20787276
复制相似问题