如何在推文光标搜索中传递用户定义的字符串

how to pass user defined string in tweet cursor search

Q) 如何在 tweet_cursor 搜索中传递用户定义的字符串 我正在尝试通过在 a 中输入并传递变量 a 来根据用户获取推文,请帮助

目前它只搜索一个字面意思而不是变量a 由用户定义

`导入文本块 将 pandas 导入为 pd 将 matplotlib.pyplot 导入为 plt 导入重新

api_key = 'xxxxxxxxxxxx'
api_key_secret = 'xxxxxxxxxxx'
access_token = 'xxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxx'

authenticator = tweepy.OAuthHandler(api_key, api_key_secret)
authenticator.set_access_token(access_token, access_token_secret)

api = tweepy.API(authenticator, wait_on_rate_limit=True)

a=input(print("enter player name"))

search= f'#(a) -filter:retweets lang:en'

tweet_cursor = tweepy.Cursor(api.search_tweets, q=search, tweet_mode='extended').items(100)

tweets = [tweet.full_text for tweet in tweet_cursor]

tweets_df = pd.DataFrame(tweets, columns=['Tweets'])

for _, row in tweets_df.iterrows():
    row['tweets'] = re.sub('https\S+', '', row['Tweets'])
    row['tweets'] = re.sub('#\S+', '', row['Tweets'])
    row['tweets'] = re.sub('@\S+', '', row['Tweets'])
    row['tweets'] = re.sub('\n', '', row['Tweets'])
print(tweets_df)
print(tweets)


tweets_df['Polarity'] = tweets_df['Tweets'].map(lambda tweet: textblob.TextBlob(tweet).sentiment.polarity)
tweets_df['Result'] = tweets_df['Polarity'].map(lambda pol: '+' if pol > 0 else '-')

positive = tweets_df[tweets_df.Result == '+'].count()['Tweets']
negative = tweets_df[tweets_df.Result == '-'].count()['Tweets']

print(positive)
print(negative)
langs = ['Positive', 'Negative']
students = [positive,negative]
a=plt.bar(langs,students)
a[0].set_color('g')
a[1].set_color('r')

plt.xlabel("Tweet Sentiment")
plt.ylabel("No. of Tweets")
plt.title("Sentiment Analysis")
plt.legend

plt.show() `

在 Python f-Strings 中,您必须在变量周围使用花括号。

search = f'#{a} -filter:retweets lang:en

但是这将搜索包含 主题标签 a.

的转推

如果你想从用户a的推文中搜索,你应该使用:

search = f'from:{a} -filter:retweets lang:en

有关所有搜索运算符,请参阅 Twitter API documentation