退出代码0,无错误,无输出

时间:2019-06-08 18:20:54

标签: python

我正在运行此python情绪分析,当我运行它时,没有错误,也没有输出。我是这个新手,但似乎无法通过研究发现任何原因。

我已经通过pycharms调试器运行它,并且退出代码为0。当我通过pycharms IDE或命令行运行它时,它只是运行,然后什么也没有发生。

TwitterClient(object)类:

def __init__(self):

    consumer_key = "ConsumerKey" 
    consumer_secret = "ConsumerSecret" 
    access_token = "AccessToken" 
    access_token_secret = "AccessTokenSecret" 
    try:
        self.auth = OAuthHandler(consumer_key, consumer_secret)
        self.auth.set_access_token(access_token, access_token_secret)
        self.api = tweepy.API(self.auth)
    except:
        print("Error: Authentication Failed")
def clean_tweet(self, tweet):

    return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t]) | (\w+:\ / \ / \S+)", " ", tweet).split())
def get_tweet_sentiment(self, tweet):

    analysis = TextBlob(self.clean_tweet(tweet))
    if analysis.sentiment.polarity > 0:
        return 'positive'
    elif analysis.sentiment.polarity == 0:
        return 'neutral'
    else:
        return 'negative'
def get_tweets(self, query, count=10):

    tweets = []
    try:
        fetched_tweets = self.api.search(q="Microsoft", count=5)
        for tweet in fetched_tweets:
            parsed_tweet = {}
            parsed_tweet['text'] = tweet.text
            parsed_tweet['sentiment'] = self.get_tweet_sentiment(tweet.text)
            if tweet.retweet_count > 0:
                if parsed_tweet not in tweets:
                    tweets.append(parsed_tweet)
            else:
                tweets.append(parsed_tweet)
        return tweets
    except tweepy.TweepError as e:
        print("Error : " + str(e))
def main():
    api = TwitterClient()
    tweets = api.get_tweets(query='Microsoft', count=50)
    ptweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive']
    print("Positive tweets percentage: {} %".format(100 * len(ptweets) / len(tweets)))
    ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative']
    print("Negative tweets percentage: {} %".format(100 * len(ntweets) / len(tweets)))
    print("Neutral tweets percentage: {} % \
      ".format(100 * len(tweets - ntweets - ptweets) / len(tweets)))
    print("\n\nPositive tweets:")
    for tweet in ptweets[:10]:
        print(tweet['text'])
    print("\n\nNegative tweets:")
    for tweet in ntweets[:10]:
        print(tweet['text'])
    if __name__ == "__main__":
        main()

1 个答案:

答案 0 :(得分:0)

if __name__ == "__main__":
        main()

必须在“全局范围”内。缩进不正确,导致if语句出现在主函数中。