如何解决如何从Python中的twitter数据获取.csv中的所有属性?
我正在尝试获取我的Twitter帐户的数据,并希望将其导出到.csv文件。我有以下相同的代码。通过使用此代码,我仅获得3个属性,例如 ID,create_at和Text 。我想获取.csv文件中的所有属性。我该怎么办?
谢谢。
import pandas as pd
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import time
import csv
#from tweepy import twitter_credentials
def get_all_tweets(screen_name):
#Twitter only allows access to a users most recent 3240 tweets with this method
#authorize twitter,initialize tweepy
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_key,access_secret)
api = tweepy.API(auth)
#initialize a list to hold all the tweepy Tweets
alltweets = []
#make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name = screen_name,count=200)
#save most recent tweets
alltweets.extend(new_tweets)
#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
#keep grabbing tweets until there are no tweets left to grab
while len(new_tweets) > 0:
print(f"getting tweets before {oldest}")
#all subsiquent requests use the max_id param to prevent duplicates
new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
#save most recent tweets
alltweets.extend(new_tweets)
#update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
print(f"...{len(alltweets)} tweets downloaded so far")
#transform the tweepy tweets into a 2D array that will populate the csv
outtweets = [[tweet.id_str,tweet.created_at,tweet.text] for tweet in alltweets]
#write the csv
with open(f'new_{screen_name}_tweets.csv','w',encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","text"])
writer.writerows(outtweets)
pass
解决方法
我认为可能是因为您只是试图从结果中获取这些信息。
对所得Status
对象的引用为here
def get_all_tweets(screen_name):
#Twitter only allows access to a users most recent 3240 tweets with this method
#authorize twitter,initialize tweepy
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_key,access_secret)
api = tweepy.API(auth)
#initialize a list to hold all the tweepy Tweets
alltweets = []
#make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name = screen_name,count=200)
#save most recent tweets
alltweets.extend(new_tweets)
#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
#keep grabbing tweets until there are no tweets left to grab
while len(new_tweets) > 0:
print(f"getting tweets before {oldest}")
#all subsiquent requests use the max_id param to prevent duplicates
new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
#save most recent tweets
alltweets.extend(new_tweets)
#update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
print(f"...{len(alltweets)} tweets downloaded so far")
#transform the tweepy tweets into a 2D array that will populate the csv
outtweets = [[tweet.created_at,tweet.id,tweet.id_str,tweet.text,tweet.entities,tweet.source,tweet.source_url,tweet.in_reply_to_status_id,tweet.in_reply_to_status_id_str,tweet.in_reply_to_user_id,tweet.in_reply_to_user_id_str,tweet.in_reply_to_screen_name,tweet.user.id,tweet.user.name,tweet.geo,tweet.coordinates,tweet.place,tweet.contributors,tweet.is_quote_status,tweet.retweet_count,tweet.favorite_count,tweet.favorited,tweet.retweeted,tweet.lang] for tweet in alltweets]
#write the csv
with open(f'new_{screen_name}_tweets.csv','w',encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(["created_at","id","id_str","text","entities","source","source_url","in_reply_to_status_id","in_reply_to_status_id_str","in_reply_to_user_id","in_reply_to_user_id_str","in_reply_to_screen_name","user_id","user_name","geo","coordinates","place","contributors","is_quote_status","retweet_count","favorite_count","favorited","retweeted","lang"])
writer.writerows(outtweets)
pass
对于用户,因为它是对象,所以可以通过添加逗号和属性名称来访问其属性,例如:tweet.user.id
或tweet.user.name
。
可以在Twitter API documentation中找到属性名称的列表。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。