如何使此脚本每小时循环一次

编程入门 行业动态 更新时间:2024-10-25 14:22:56
本文介绍了如何使此脚本每小时循环一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在创建一个每60分钟发布一次covid数字的漫游器,我已经完成了,但是idk如何使其重复.任何的想法?我想到的是一个小项目,这是我要做的最后一件事. (如果您使用解决方案中的内容回答出版物中的代码,那将非常酷)

I'm creating a bot that posts every 60 minutes covid numbers, I have it finished but idk how to make it repeat. Any idea? It's a little project that I have in mind and it's the last thing I have to do. (if you answer the code from the publication with inside the solution it would be very cool)

import sys CONSUMER_KEY = 'XXXX' CONSUMER_SECRET = 'XXXX' ACCESS_TOKEN = 'XXXX' ACCESS_TOKEN_SECRET = 'XXXX' import tweepy import requests from lxml import html def create_tweet(): response = requests.get('www.worldometers.info/coronavirus/') doc = html.fromstring(response.content) total, deaths, recovered = doc.xpath('//div[@class="maincounter-number"]/span/text()') tweet = f'''Coronavirus Latest Updates Total cases: {total} Recovered: {recovered} Deaths: {deaths} Source: www.worldometers.info/coronavirus/ #coronavirus #covid19 #coronavirusnews #coronavirusupdates #COVID19 ''' return tweet if __name__ == '__main__': auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) # Create API object api = tweepy.API(auth) try: api.verify_credentials() print('Authentication Successful') except: print('Error while authenticating API') sys.exit(5) tweet = create_tweet() api.update_status(tweet) print('Tweet successful')

推荐答案

您只需在代码末尾添加此语句

You can simply add this statement at the end of the code

sleep(3600)

如果您希望它不断运行,可以这样做:

If you want it to run endlessly, you can do it like this:

while True: insert your main code here sleep(3600)

更多推荐

如何使此脚本每小时循环一次

本文发布于:2023-11-27 07:04:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637074.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:每小时   脚本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!