Tweepy的Twitter Bot-Python [英] Twitter Bot with Tweepy - Python

查看:214
本文介绍了Tweepy的Twitter Bot-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个Twitter Bot,该机器人在其邮件中用嗨___,我是爸爸!"回复"im"的人.

I have been working on a Twitter Bot which replies to people with "im" in their message with "Hi ___, I'm Dad!".

我以前曾经让它工作,但是后来我的电脑死了,丢失了所有文件.

I used to have it working before, but then my computer died and lost all of it's files.

几年前,我写了原始的bot,而且有一段时间没看过Tweepy了.

I wrote the original bot a couple years ago, and I haven't looked at Tweepy in a while.

我很确定自己100%都知道了-没有错误弹出,但是该机器人无法正常工作,我也不知道为什么.

I was pretty sure I had it 100% figured out - and no errors popped up, but the bot isn't working and I don't know why.

我可以正常登录,但是实际的回复部分出了点问题.

I can log in just fine, but something is wrong with the actual replying part.

有人可以帮我吗?

import tweepy as tt
import time


#login credentials twitter account
consumer_key = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
consumer_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_token = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'

#login
auth = tt.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tt.API(auth)
search_results = api.search("r'\bi\'?m\s+(.*)',re.IGNORECASE,text", count=100)

user = api.me()
print(user.name)


#reply
for tweet in search_results:

# answer to the hashtag tweet
    if len(reply) > 0:
        c=message.content
        c=c.replace("im ","")
        answer="@"+user+" Hi " + c + ", I'm Dad!"
        print ("Reply:",answer)
# tweet and pause not to stress twitter
        twitter.update_status(status=answer,in_reply_to_status_id=id)
        time.sleep(300) #every 5 minutes

推荐答案

如果您希望继续使用r(egular)e(xpression)模块,则可能要import re,但据我所知,并且根据其他人在stackoverflow上的回答,Twitter不支持使用RE来搜索推文(目前).我可以证明这一点,因为我每周都收集推文,而重新搜索失败.

if you wish to continue using the r(egular)e(xpression) module, you might want to import re but as far as I know, and also based on answers of others here on stackoverflow, Twitter doesn't support searching of tweets using RE (as of the moment). I can attest to this, since I'm gathering tweets weekly, and re searches fail.

尝试使用api.update_status( ... )而不是twitter.update_status( ... ).请注意,update_status()api对象的方法.另外,请注意,您的id尚未实例化或没有初始值.

try using api.update_status( ... ) instead of twitter.update_status( ... ). take note that update_status() is a method of the api object. also, please do note that your id is uninstantiated or has no initial value.

这是我的Twitter收集脚本中的一个片段,用于进一步的上下文,在我的实现中,我使用csvwriter将多行推文保存在一个csv中.

Here's a snippet from my twitter gathering script for further context, in my implementation, I save rows of tweets in a csv using csvwriter.

for tweet in tweepy.Cursor(api.search, q='twitter').items():  
    csvWriter.writerow([tweet.text.encode('utf8'),
                        tweet.user.screen_name, tweet.created_at,
                        tweet.retweet_count, tweet.favorite_count,
                        tweet.user.location.encode('utf8')], tweet.user.id)

就像您在几年前(以及最近)也尝试使用RE一样,我可以确认Twitter搜索/查询尚不支持RE. (很遗憾,我知道:/).但这就是数据/tweet预处理的来源.

Like you I also tried using RE a few years back, (and just recently), I could confirm that RE's not yet supported in twitter searches/queries. (Sad I know :/ ). But that's where data / tweet preprocessing comes in.

我想提出的另一点是,您无法获取一条推文得到的答复总数(我假设像我一样,您正在使用标准api(不是高级或企业级).此链接可获取有关Reply_count鸣叫的上下文功能.

Another point i'd like to make is, you can't retrieve the total number of replies a tweet gets (i'm assuming that like me you're using a standard api (not premium or enterprise).. see this link for context about the reply_count tweet feature.

对于您的情况,我建议使用以下代码搜索"您想要的推文,我使用tweepy的光标执行api.search,后跟q(uery).查询值基本上类似于Twitter的搜索"栏.尽管我的解决方案实际上不是您的解决方案,但是您可以根据需要从我的代码中进行选择.

for your case, I would suggest the following code to 'search' your intended tweets, I used tweepy's cursor to do an api.search, followed by the q(uery). the query value is basically like the 'search' bar of twitter. although my solution isn't actually your solution, you can pick up from my code if you like.

import tweepy as tt
import time
import sys
reload(sys)
sys.setdefaultencoding('utf8')


#login credentials twitter account
consumer_key = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
consumer_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_token = '-NOT GOING TO PUT THE ACTUAL KEY IN-'
access_secret = '-NOT GOING TO PUT THE ACTUAL KEY IN-'

#login
auth = tt.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tt.API(auth)
search_query = "hi I'm Dad"
user = api.me()
print(user.name)

max_tweets = 100

for tweet in tweepy.Cursor(api.search, q=search_query).items(max_tweets):
    c=tweet.text.encode('utf8')
    c=c.replace("im ","")
    answer="@"+tweet.user.screen_name+" Hi " + c + ", I'm Dad!"
    print ("Reply:",answer)
    api.update_status(status=answer,in_reply_to_status_id=tweet.id)
    time.sleep(300) #every 5 minutes

/ogs

这篇关于Tweepy的Twitter Bot-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆