简体   繁体   English

输入输入后,以下代码不执行(打印推文),是否有原因?

[英]Is there a reason why the following code does not execute (print the tweets) after taking input?

So basically I want to print a set number of tweets related to a topic that user enters but when I run the following code after giving in the input nothing happens, I see no output after that. 因此,基本上我想打印与用户输入的主题相关的一组推文,但是当我在没有输入任何内容的情况下运行以下代码时,此后没有任何输出。 I would be really grateful if you could tell me why :-) 如果您能告诉我原因,我将不胜感激:-)

I tried regenerating the access token keys and then again copy pasting it but the problem still persists 我尝试重新生成访问令牌密钥,然后再次复制粘贴它,但是问题仍然存在

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)

this is what my console is showing after execution 这是执行后我的控制台显示的内容

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]:

(It does not print the tweets) (它不打印推文)

Your language needs to be "en". 您的语言必须为“ en”。 This works: 这有效:

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)

Results of a test run: 测试结果:


Python 3.7.4 (default, Jul 9 2019, 00:06:43) [GCC 6.3.0 20170516] on linux Linux上的Python 3.7.4(默认值,2019年7月9日,00:06:43)[GCC 6.3.0 20170516]

Enter keyword/hashtag to search about : kendrick 输入关键字/标签来搜索:kendrick

How many tweets do you wanna print : 2 您想打印多少条推文:2

Tweets with kendrick : 与kendrick的推文:

RT @flwrrb0y: them: you can't even dance to kendrick lamar RT @ flwrrb0y:他们:你甚至不能和肯德里克·拉马尔跳舞

me: RT @Blaqboimagic: Calling Kendrick overrated is unacceptable 我:RT @Blaqboimagic:叫肯德里克高估是不可接受的

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM