python-如何从Twitter搜索API创建熊猫数据框?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-如何从Twitter搜索API创建熊猫数据框?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2779字,纯文字阅读大概需要4分钟。
内容图文
![python-如何从Twitter搜索API创建熊猫数据框?](/upload/InfoBanner/zyjiaocheng/695/fb434137e2b04e5f8c9ea32014698eff.jpg)
我正在使用Twitter Search API,该API返回字典.我的目标是根据响应字典中的键列表创建一个数据框.
此处的API响应示例:Example Response
我在状态字典中有一个键列表
keys = ["created_at", "text", "in_reply_to_screen_name", "source"]
我想遍历Statuses词典中返回的每个键值,并将它们放在以键为列的数据框中.
目前,有一些代码可以循环遍历单个键,然后分配给列表,然后追加到数据框,但是希望有一种方法可以一次完成多个键.当前代码如下:
#w is the word to be queired
w = 'keyword'
#count of tweets to return
count = 1000
#API call
query = twitter.search.tweets(q= w, count = count)
def data_l2 (q, k1, k2):
data = []
for results in q[k1]:
data.append(results[k2])
return(data)
screen_names = data_l3(query, "statuses", "user", "screen_name")
data = {'screen_names':screen_names,
'tweets':tweets}
frame=pd.DataFrame(data)
frame
解决方法:
在使用Twitter API时,我将分享我想到的一个更通用的解决方案.假设您要在名为my_ids的列表中获取要获取的tweet的ID:
# Fetch tweets from the twitter API using the following loop:
list_of_tweets = []
# Tweets that can't be found are saved in the list below:
cant_find_tweets_for_those_ids = []
for each_id in my_ids:
try:
list_of_tweets.append(api.get_status(each_id))
except Exception as e:
cant_find_tweets_for_those_ids.append(each_id)
然后在此代码块中,我们隔离已下载的每个扭曲状态对象的json部分,并将它们全部添加到列表中….
my_list_of_dicts = []
for each_json_tweet in list_of_tweets:
my_list_of_dicts.append(each_json_tweet._json)
…然后我们将此列表写入txt文件:
with open('tweet_json.txt', 'w') as file:
file.write(json.dumps(my_list_of_dicts, indent=4))
现在,我们将从tweet_json.txt文件创建一个DataFrame(我添加了一些与我正在使用的用例相关的键,但是您可以添加特定的键):
my_demo_list = []
with open('tweet_json.txt', encoding='utf-8') as json_file:
all_data = json.load(json_file)
for each_dictionary in all_data:
tweet_id = each_dictionary['id']
whole_tweet = each_dictionary['text']
only_url = whole_tweet[whole_tweet.find('https'):]
favorite_count = each_dictionary['favorite_count']
retweet_count = each_dictionary['retweet_count']
created_at = each_dictionary['created_at']
whole_source = each_dictionary['source']
only_device = whole_source[whole_source.find('rel="nofollow">') + 15:-4]
source = only_device
retweeted_status = each_dictionary['retweeted_status'] = each_dictionary.get('retweeted_status', 'Original tweet')
if retweeted_status == 'Original tweet':
url = only_url
else:
retweeted_status = 'This is a retweet'
url = 'This is a retweet'
my_demo_list.append({'tweet_id': str(tweet_id),
'favorite_count': int(favorite_count),
'retweet_count': int(retweet_count),
'url': url,
'created_at': created_at,
'source': source,
'retweeted_status': retweeted_status,
})
tweet_json = pd.DataFrame(my_demo_list, columns = ['tweet_id', 'favorite_count',
'retweet_count', 'created_at',
'source', 'retweeted_status', 'url'])
内容总结
以上是互联网集市为您收集整理的python-如何从Twitter搜索API创建熊猫数据框?全部内容,希望文章能够帮你解决python-如何从Twitter搜索API创建熊猫数据框?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。