我一直在使用Tweepy在Python中做一个小项目。代码的目的是对推文进行排序。如果一条推文包含我列表中的一个词,它会保存在一个正面文件中,或者保存在负面文件中。
我也对回复或转发不感兴趣。
我尝试了两种不同的方法,最新的一种是使用 .find((。我不明白。当我将推文作为字符串抓取并从中获取一个单词作为字符串并使用该功能时,它就可以完成工作。
但是在我的脚本中,它总是返回 -1。欢迎任何提示和想法。顺便说一句,我是蟒蛇的菜鸟。
class SortedTweet(object):
tweet_text = ""
is_positive = False
is_retweeted = False
is_reply = False
created_at = None
id = ""
def __init__(self, tweet, is_positive):
self.tweet_text = tweet.full_text
self.created_at = tweet.created_at
self.is_retweeted = hasattr(tweet, 'retweeted_status') is True
self.is_reply = tweet.in_reply_to_status_id is not None
self.is_positive = is_positive
self.id = tweet.id_str
def sort_tweets(currentTweetList):
with open('list.txt','r') as keyword_file:
for keyword in keyword_file:
for tweet in currentTweetList:
s = tweet.tweet_text.lower()
if ((s.find(keyword) != -1) & (tweet.is_reply == False) & (tweet.is_retweeted == False)):
tweet.is_positive = True
return currentTweetList
我需要添加命令 .strip((,因为每次它读取一行时,它也包含"enter"。所以难怪没有比赛。