首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过在tweepy上进行简单搜索来创建CSV?

如何通过在tweepy上进行简单搜索来创建CSV?
EN

Stack Overflow用户
提问于 2017-07-04 02:19:22
回答 1查看 1.7K关注 0票数 0

我正在尝试编写一个脚本,以.CSV格式下载推特搜索,但是,我的代码有一个错误,有什么帮助吗?

代码语言:javascript
复制
import tweepy
import csv
import pandas as pd
####input your credentials here
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)
#####United Airlines
# Open/Create a file to append data
csvFile = open('test.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)

for tweet in tweepy.Cursor(api.search,q="petya",count=100,
                           since="2017-04-03").items():
        print ("ID:", tweet.id)
        print ("User ID:", tweet.user.id)
        print ("Text:", tweet.text)
        print ("Created:", tweet.created_at)
        print ("Geo:", tweet.geo)
        print ("Contributors:", tweet.contributors)
        print ("Coordinates:", tweet.coordinates) 
        print ("Favorited:", tweet.favorited)
        print ("In reply to screen name:", tweet.in_reply_to_screen_name)
        print ("In reply to status ID:", tweet.in_reply_to_status_id)
        print ("In reply to status ID str:", tweet.in_reply_to_status_id_str)
        print ("In reply to user ID:", tweet.in_reply_to_user_id)
        print ("In reply to user ID str:", tweet.in_reply_to_user_id_str)
        print ("Place:", tweet.place)
        print ("Retweeted:", tweet.retweeted)
        print ("Retweet count:", tweet.retweet_count)
        print ("Source:", tweet.source)
        print ("Truncated:", tweet.truncated)

    # Write a row to the CSV file. I use encode UTF-8
    csvWriter.writerow([tweet.created_at, tweet.user.id, tweet.id, tweet.geo, tweet.text, tweet.contributors, tweet.favorited, tweet.source, tweet.retweeted, tweet.in_reply_to_screen_name, eet.in_reply_to_status_id_str('utf-8')])
    print tweet.created_at, tweet.user.id, tweet.id, tweet.geo, tweet.text, tweet.contributors, tweet.favorited, tweet.source, tweet.retweeted, tweet.in_reply_to_screen_name, eet.in_reply_to_status_id_str
csvFile.close()

我认为问题出在csvWriter的最后部分,也许我在一行中放了很多文本?正如我之前所说的,我是个新手,需要大量的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-04 15:57:07

我认为最简单的解决方案是使用熊猫(有趣的是,您导入了它,但没有使用它)。

一个有效的解决方案可能如下所示:

代码语言:javascript
复制
import tweepy
import pandas as pd
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)

# create list to append tweets to
tweets = []

# append all tweet data to list
for tweet in tweepy.Cursor(api.search,q="petya",count=100,
                       since="2017-04-03").items():
    tweets.append(tweet)

# convert 'tweets' list to pandas.DataFrame
tweets_df = pd.DataFrame(vars(tweets[i]) for i in range(len(tweets)))

# define file path (string) to save csv file to
FILE_PATH = </path/to/file.csv>

# use pandas to save dataframe to csv
tweets_df.to_csv(FILE_PATH)

然后,你就完蛋了!

请注意,如果您只想选择一组特定的tweet,您可以只创建一个列表,然后设置数据帧的子集。

例如(在将tweet转换为pandas.DataFrame的步骤之后):

代码语言:javascript
复制
# define attributes you want
tweet_atts = [
'text', 'created_at', 'favorite_count',
'lang', 'retweet_count', 'source',
'in_reply_to_user_id_str', 'retweeted',
'id'
]

# subset dataframe
tweets_df = tweets_df[tweets_atts]

# save resulting df to csv
tweets_df.to_csv(FILE_PATH)

如果您需要更多帮助,请随时回复!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44891815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档