Tweepy不返回任何结果

问题描述

下面是代码。当我运行代码时,它什么也没做。我认为问题出在标签中。因为当我使用诸如 Facebook 之类的其他字词时,它可以工作,但不适用于其他小型公司。另外,它不会在 out.csv

中保存任何结果
import tweepy
import credentials

# authorization tokens
consumer_key = credentials.API_KEY
consumer_secret = credentials.API_SECRET_KEY
access_key = credentials.ACCESS_TOKEN
access_secret = credentials.ACCESS_TOKEN_SECRET

# StreamListener class inherits from tweepy.StreamListener and overrides on_status/on_error methods.
class StreamListener(tweepy.StreamListener):
    def on_status(self,status):
        print(status)
        # if "retweeted_status" attribute exists,flag this tweet as a retweet.
        is_retweet = hasattr(status,"retweeted_status")

        # check if text has been truncated
        if hasattr(status,"extended_tweet"):
            text = status.extended_tweet["full_text"]
        else:
            text = status.text

        # check if this is a quote tweet.
        is_quote = hasattr(status,"quoted_status")
        quoted_text = ""
        if is_quote:
            # check if quoted tweet's text has been truncated before recording it
            if hasattr(status.quoted_status,"extended_tweet"):
                quoted_text = status.quoted_status.extended_tweet["full_text"]
            else:
                quoted_text = status.quoted_status.text

        # remove characters that might cause problems with csv encoding
        remove_characters = [",","\n"]
        for c in remove_characters:
            text.replace(c," ")
            quoted_text.replace(c," ")

        with open("out.csv","a",encoding='utf-8') as f:
            f.write("%s,%s,%s\n" % (status.created_at,status.user.screen_name,is_retweet,is_quote,text,quoted_text))

    def on_error(self,status_code):
        print("Encountered streaming error (",status_code,")")
        sys.exit()

if __name__ == "__main__":
    # complete authorization and initialize API endpoint
    auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
    auth.set_access_token(access_key,access_secret)
    api = tweepy.API(auth)

    # initialize stream
    streamListener = StreamListener()
    stream = tweepy.Stream(auth=api.auth,listener=streamListener,tweet_mode='extended')
    with open("out.csv","w",encoding='utf-8') as f:
        f.write("date,user,quoted_text\n")
    tags = ["Enbridge"]
    stream.filter(track=tags)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...