【问题标题】:Is there a reason why the following code does not execute (print the tweets) after taking input?以下代码在输入后没有执行(打印推文)是否有原因?
【发布时间】:2019-12-23 12:12:54
【问题描述】:

所以基本上我想打印一定数量的与用户输入的主题相关的推文,但是当我在输入后运行以下代码时没有任何反应,之后我看不到任何输出。如果您能告诉我原因,我将不胜感激:-)

我尝试重新生成访问令牌密钥,然后再次复制粘贴,但问题仍然存在

import tweepy
consumerKey = "Sgdz0quGjDDTtGbFAxWQ02E5M"
consumerSecret = "alphanumeric"
accessToken = "980878168180609024-nggEvf3WSLb1IcmmHfoCMhDNvZjbMid"
accessTokenSecret = "alpha numeric"

auth = tweepy.OAuthHandler(consumer_key=consumerKey, 
consumer_secret=consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)

searchTerm = input("Enter keyword/hashtag to search about : ")
number = int(input("How many tweets do you wanna print :  "))
tweets = tweepy.Cursor(api.search, q=searchTerm, lang= "English").items(number)

for tweet in tweets:
    print(tweet.text)

这是我的控制台在执行后显示的内容

runfile('C:/users/acer/.spyder-py3/temp.py', wdir='C:/users/acer/.spyder-py3')

Enter keyword/hastag to search about : bts

How many tweets do you wanna print :  5

In [14]:

(它不打印推文)

【问题讨论】:

    标签: python-3.x twitter-oauth tweepy sentiment-analysis textblob


    【解决方案1】:

    您的语言必须是“en”。这有效:

    searchTerm = input("Enter keyword/hashtag to search about : ")
    number = int(input("How many tweets do you wanna print :  "))
    
    print ("Tweets with ", searchTerm, ": ")
    
    for result in tweepy.Cursor(api.search, q=searchTerm, lang="en").items(number):
      print( result.text)
    

    测试运行结果:


    Python 3.7.4(默认,2019 年 7 月 9 日,00:06:43) [GCC 6.3.0 20170516] 在 Linux 上

    输入关键字/标签以搜索:kendrick

    你想打印多少条推文:2

    与肯德里克的推文:

    RT @flwrrb0y:他们:你甚至不能和肯德里克·拉马尔跳舞

    我: RT @Blaqboimagic:称 Kendrick 被高估是不可接受的

    【讨论】:

    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    相关资源
    最近更新 更多