python - 如何使用for循环从DataFrame迭代所有推文的ID并将推文添加到DataFrame的空列
问题描述
我有一个 DataFrame,其中包含 twitter 的推文 ID 和情绪,我将使用它们进行情绪分析。
我想使用“tweepy library”搜索所有 tweet_id 的推文,并将它们同时添加到这个 DataFrame 中。注意:我使用 try-except 因为很少有推文是私有的或已删除的,因此在抓取成千上万条推文时不会引发错误。
#Code:
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)
# calling the api
api = tweepy.API(auth)
#Below for loop does not work. Maybe its not passing each tweet_id to the status=api.get_status()
#This tweepy code is correct. because i have tried this with list (by converting df's tweet_id #into list and then iterating those list to get status of each tweets but by using df it doesnt #work.
for index, row in df.iterrows():
try:
status = api.get_status(row['tweet_id'])
except tweepy.TweepError:
text = status.text
df['tweets'].append(text)
解决方案
推荐阅读
- flutter - 如何反映从父组件到子组件的更改
- debian - crontab中的rsync不执行
- spring - 在 Spring Boot 中使用过滤器时响应需要很长时间
- python - 更改现有 matplotlib 图的颜色图
- c - %lo(source)($6) 和 .frame 在汇编代码中是什么意思?
- kotlin - 带有kotlin的黄瓜参数类型
- graphql - 如何使我的 hasura 动作准备好用于我的 ci / cd 测试?
- c - 如何知道 Linux 中看门狗重启的原因?
- python - 为什么当我输入一个值时程序会退出?
- vue.js - 当我使用 Axios 获取数据时,我的 JS 代码不起作用