Python 将lookup_users方法的结果保存在json文件中
我正在尝试使用tweepy by user screen name获取用户元数据,并将结果保存为JSON文件。这是我的密码Python 将lookup_users方法的结果保存在json文件中,python,python-3.x,twitter,tweepy,Python,Python 3.x,Twitter,Tweepy,我正在尝试使用tweepy by user screen name获取用户元数据,并将结果保存为JSON文件。这是我的密码 import tweepy from tweepy import Stream from tweepy.streaming import StreamListener from tweepy import OAuthHandler CONSUMER_KEY = 'xxx' CONSUMER_SECRET = 'xxx' ACCESS_KEY = 'xxx' ACCESS_
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
CONSUMER_KEY = 'xxx'
CONSUMER_SECRET = 'xxx'
ACCESS_KEY = 'xxx'
ACCESS_SECRET = 'xxx'
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output
def on_data(self, data):
print (data)
return True
def on_error(self, status):
print (status)
#search
api = tweepy.API(auth)
twitterStream = Stream(auth,TweetListener())
#name is list contains user screen names
test = api.lookup_users(screen_names= name)
for user in test:
print (user.screen_name)
print (user.name)
print (user.description)
print (user.followers_count)
print (user.statuses_count)
我的代码运行时没有任何错误,正如您所看到的,我正在打印数据,但我的目的是将它们保存在JSON文件中
我尝试了不同的密码,但都不适合我。那么,有什么帮助吗?JSON只是一种以特定格式保存数据的方法。 为了保存数据,您首先需要正确存储数据,然后将其转储。 格式是dict,它保存由键分隔的数据,每个键都是一个映射。 在您的情况下,我选择“用户”作为密钥。 “用户”中的每个用户都有您列表中的键(名称、描述等)和值 您需要这样做:
import json
data = {'users': []}
for user in test:
data['users'].append({
'screen_name': user.screen_name,
'name': user.name,
'description': user.description,
'followers_count': user.followers_count,
'statuses_count': user.statuses_count
})
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
JSON只是一种以特定格式保存数据的方法。 为了保存数据,您首先需要正确存储数据,然后将其转储。 格式是dict,它保存由键分隔的数据,每个键都是一个映射。 在您的情况下,我选择“用户”作为密钥。 “用户”中的每个用户都有您列表中的键(名称、描述等)和值 您需要这样做:
import json
data = {'users': []}
for user in test:
data['users'].append({
'screen_name': user.screen_name,
'name': user.name,
'description': user.description,
'followers_count': user.followers_count,
'statuses_count': user.statuses_count
})
with open('data.txt', 'w') as outfile:
json.dump(data, outfile)
非常感谢。它起作用了,但因为大多数描述和其他字段都是阿拉伯语,所以在文件{“screen_name”:“AliAbd87895756”,“name”:“\u0639\u0644\u064a\u0646\u0635\u0627\u0631”、“description”:“\u200f\u200f\u200f\u200f\u200f\u0627\u0644\u0628\u0648\u0644\u064a\u0633\u0643\u83d\u4d\u83d\u83a”中就是这样显示的,“followers_count”:89,“statuses_count”:1038}有没有办法将其保存为阿拉伯语尝试在文件的beging中添加以下内容:#--coding:utf-8--另外,将转储更改为如下:with open('data.txt',w',encoding='utf8'))as outfile:感谢您的回复,但我仍然得到相同的输出,问题没有得到解决谢谢您。它起作用了,但因为大多数描述和其他字段都是阿拉伯语,所以在文件{“screen_name”:“AliAbd87895756”,“name”:“\u0639\u0644\u064a\u0646\u0635\u0627\u0631”,“description”:\u200f\u200f\u200f\u200f\u200f\u200f\u0627\u0644\u0628\u0648\u0644\u064a\u0633\644\u0643\ud83d\udc4d\ud83e\udd1a,“followers_count”:89,“status_count”:1038}是否有任何方法将它们保存为阿拉伯语请尝试在文件的beging中添加以下内容:#——编码:utf-8——另外,将转储更改为如下:打开时('data.txt','w',encoding='utf8')作为输出文件:感谢您的回复,但我仍然得到相同的输出,问题没有解决