我正在尝试从Twitter检索数据,使用Tweepy获取在命令行输入的用户名.我想提取有关状态和用户的相当多的数据,所以提出以下内容:
请注意,我正在导入所有必需的模块,并且有oauth +键(这里没有包含它),文件名是正确的,只是更改了:
# define user to get tweets for. accepts input from user
user = tweepy.api.get_user(input("Please enter the twitter username: "))
# Display basic details for twitter user name
print (" ")
print ("Basic information for", user.name)
print ("Screen Name:", user.screen_name)
print ("Name: ", user.name)
print ("Twitter Unique ID: ", user.id)
print ("Account created at: ", user.created_at)
timeline = api.user_timeline(screen_name=user, include_rts=True, count=100)
for tweet in timeline:
print ("ID:", tweet.id)
print ("User ID:", tweet.user.id)
print ("Text:", tweet.text)
print ("Created:", tweet.created_at) …Run Code Online (Sandbox Code Playgroud) 似乎使用tweepy只能使用user_timeline方法获得200条推文。
class Twitter_User():
def __init__(self,id,count=200):
self.id = id
self.count = count
self.data = None
def get_tweets(self):
store_tweets = api.user_timeline(self.id, count=self.count)
simple_list = []
for status in store_tweets:
array = [status._json["text"].strip(), status._json["favorite_count"], status._json["created_at"],status._json["retweet_count"],[h["text"] for h in status._json["entities"]["hashtags"]]]
simple_list.append(array)
self.data = pd.DataFrame(simple_list, columns=["Text", "Like", "Created at","Retweet","Hashtags"])
self.data = self.data[~self.data["Text"].str.startswith('RT')]
return self.data
def __repr__(self):
id = api.get_user(self.id)
return id.screen_name
Run Code Online (Sandbox Code Playgroud)
如果我将self.count设置为大于200的数字,则始终会得到一个包含200行的数据框,相反,如果我输入的数字较小,则会得到正确的行数。我不知道,有限制,还是我必须使用其他方法?