简体   繁体   English

为什么我的推特机器人发布的内容超出预期?

[英]Why does my twitter bot post more than expected?

I am using the code time.sleep(3600) and it is tweeting more than every 3600 seconds.我正在使用代码time.sleep(3600)并且它每 3600 秒就会发推一次。 Why is this happening?为什么会这样?

Currently it is tweeting at 9 minutes past, then 32 minutes past.目前它在 9 分钟后发推文,然后是 32 分钟后。

Edit:编辑:

Here is the code.这是代码。 The only other reason this could be happening is that this may be running in multiple instances accidentally.这可能发生的唯一另一个原因是这可能会意外地在多个实例中运行。 I will check that.我会检查的。

# tweepy will allow us to communicate with Twitter, time will allow us to set how often we tweet
import tweepy, time

#enter the corresponding information from your Twitter application management:
CONSUMER_KEY = 'mykey' #keep the quotes, replace this with your consumer key
CONSUMER_SECRET = 'mykey' #keep the quotes, replace this with your consumer secret key
ACCESS_TOKEN = 'my-my' #keep the quotes, replace this with your access token
ACCESS_SECRET = 'mykey' #keep the quotes, replace this with your access token secret


# configure our access information for reaching Twitter
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)

# access Twitter!
api = tweepy.API(auth)

# open our content file and read each line
filename=open('content.txt')
f=filename.readlines()
filename.close()

# for each line in our contents file, lets tweet that line out except when we hit a error
for line in f:
    try:
        api.update_status(line)
        print("Tweeting!")
    except tweepy.TweepError as err:
        print(err)
    time.sleep(3600) #Tweet every hour
print("All done tweeting!")

This may be caused by your module not being protected from running when imported.这可能是由于您的模块在导入时未受到保护而无法运行。 That means every time your module is imported, (could happen on这意味着每次导入模块时,(可能发生在

from package import *   

), your code is interpreted and a new loop is created. ),您的代码将被解释并创建一个新循环。


You could ensure your code is run only when you want it to run with this :您可以确保您的代码仅在您希望它运行时运行:

Make a function from your code, let's name it main().从您的代码中创建一个函数,我们将其命名为 main()。

You can then check if your module is called as a script.然后,您可以检查您的模块是否作为脚本调用。

def main():
    # tweepy will allow us to communicate with Twitter, time will allow us to set how often we tweet
    import tweepy, time

    #enter the corresponding information from your Twitter application management:
    CONSUMER_KEY = 'mykey' #keep the quotes, replace this with your consumer key
    CONSUMER_SECRET = 'mykey' #keep the quotes, replace this with your consumer secret key
    ACCESS_TOKEN = 'my-my' #keep the quotes, replace this with your access token
    ACCESS_SECRET = 'mykey' #keep the quotes, replace this with your access token secret


    # configure our access information for reaching Twitter
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)

    # access Twitter!
    api = tweepy.API(auth)

    # open our content file and read each line
    filename=open('content.txt')
    f=filename.readlines()
    filename.close()

    # for each line in our contents file, lets tweet that line out except when we hit a error
    for line in f:
        try:
            api.update_status(line)
            print("Tweeting!")
        except tweepy.TweepError as err:
            print(err)
        time.sleep(3600) #Tweet every hour
    print("All done tweeting!")

if __name__ == "__main__":
    main()

If you have to use your code from another script, you can use如果您必须使用另一个脚本中的代码,则可以使用

from your_module import main
main()

Or from a command line :或者从命令行:

python -m your_module

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

相关问题 为什么此正则表达式拆分返回的组件多于预期? - Why does this regex split return more components than expected? 为什么我的正则表达式搜索结果超出预期,为什么我的循环没有删除某些重复项? - Why is my Regex search yielding more than expected and why isn't my loop removing certain duplicates? 为什么我的 twitter 机器人只推一个单词或一个字母,而不是一个完整的句子? - Why does my twitter bot only tweet one word or letter, and not a full sentence? 为什么我的代码在文件中重复多次 - Why does my code repeat itself more than once in a file 为什么我的for循环运行的次数超过我指定的次数? - Why does my for loop run more times than I specify? 为什么 Pandas read_table 说它在我的文本文件行中看到的令牌比预期的多一个? - Why is Pandas read_table saying that it saw one more token than expected in my text file line? 为什么我的 Twitter 机器人没有发布它的“.update_status”? - Why is my Twitter bot not posting it's ".update_status"? 如果我执行一个以上的纪元,为什么我的keras模型会终止并冻结我的笔记本? - Why does my keras model terminate and freeze my notebook if I do more than one epoch? 为什么我的 discord 机器人会引发此错误? - Why does my discord bot raise this error? 为什么我的 Python 机器人不工作? (PyAutoGUI) - Why does my Python bot not work? (PyAutoGUI)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM