以可用形式提取 Tweepy 中的数据 (JSON)
Extracting data in Tweepy in useable form (JSON)
我正在使用 Tweepy 收集关注者的数据。我能够打印返回的数据,但无论我尝试什么,我都无法将数据以可重用的形式保存到文件中。
下面是我成功导出到 txt 文件的方法,但是当我将该文件调用到变量中时,它设置了一个字符数组,并且没有将其视为可用数组。
这是 Tweepy 调用:
import sys
import tweepy
import json
# Key info removed
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
ids = []
for page in tweepy.Cursor(api.followers, screen_name="handle").pages():
ids.extend(page)
print ids
with open("followers.txt", 'w') as f:
f.write(str(ids))
我从中得到的文件开始于:
[User(follow_request_sent=False...
如果我用
调用数据
data = f.read()
print data[0] # returns '['
您可以先使用
保存文件
f.close()
它会将您的数据保存到文件中,然后您可以使用它
import json
from pprint import pprint
with open('followers.txt') as new_file:
new_file = json.load(new_file)
pprint(data)
您可以参考原始 python 文档 here 以了解有关 python.
文件中读取方法的更多信息
希望对您有所帮助!
每个 page
都是 list
个 User
对象。因此,当您:ids.extend(page)
.
时,您将丢失 tweepy 数据结构
试试下面的这段代码:
with open("followers.txt", 'a') as f: #open file first
for page in tweepy.Cursor(api.followers, screen_name='handle').pages():
for user_obj in page: #iterate through each User object
json.dump(user_obj._json, f) #dump each to file, f
f.write("\n") #you'll need this for Martjin's answer below to work.
感谢 Martjin Pieter 对 this question 的回答。您可以加载和离散访问您的数据。我修改他的代码片段的方式是将 jfile
附加到名为 user_jsons
的列表(这相当于您的 data
变量)。
user_jsons = []
with open("followers.txt", 'rb') as f:
for line in f:
while True:
try:
jfile = json.loads(line)
break
except ValueError:
# Not yet a complete JSON value
line += next(f)
user_jsons.append(jfile)
现在您有一个包含 json 个对象的列表...[7] 被截断了
In [7]: user_jsons[0]
Out[7]: {u'blocked_by': False,
u'blocking': False,
u'contributors_enabled': False,
u'created_at': u'Thu Jan 30 18:33:13 +0000 2014',
...
In [8]: user_jsons[0]['screen_name']
Out[8]: u'some_users_handle'
您可能会发现 ipython 笔记本 here 是非常有用的资源,尤其是第 9 章。
我正在使用 Tweepy 收集关注者的数据。我能够打印返回的数据,但无论我尝试什么,我都无法将数据以可重用的形式保存到文件中。
下面是我成功导出到 txt 文件的方法,但是当我将该文件调用到变量中时,它设置了一个字符数组,并且没有将其视为可用数组。
这是 Tweepy 调用:
import sys
import tweepy
import json
# Key info removed
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
ids = []
for page in tweepy.Cursor(api.followers, screen_name="handle").pages():
ids.extend(page)
print ids
with open("followers.txt", 'w') as f:
f.write(str(ids))
我从中得到的文件开始于:
[User(follow_request_sent=False...
如果我用
调用数据data = f.read()
print data[0] # returns '['
您可以先使用
保存文件f.close()
它会将您的数据保存到文件中,然后您可以使用它
import json
from pprint import pprint
with open('followers.txt') as new_file:
new_file = json.load(new_file)
pprint(data)
您可以参考原始 python 文档 here 以了解有关 python.
文件中读取方法的更多信息希望对您有所帮助!
每个 page
都是 list
个 User
对象。因此,当您:ids.extend(page)
.
试试下面的这段代码:
with open("followers.txt", 'a') as f: #open file first
for page in tweepy.Cursor(api.followers, screen_name='handle').pages():
for user_obj in page: #iterate through each User object
json.dump(user_obj._json, f) #dump each to file, f
f.write("\n") #you'll need this for Martjin's answer below to work.
感谢 Martjin Pieter 对 this question 的回答。您可以加载和离散访问您的数据。我修改他的代码片段的方式是将 jfile
附加到名为 user_jsons
的列表(这相当于您的 data
变量)。
user_jsons = []
with open("followers.txt", 'rb') as f:
for line in f:
while True:
try:
jfile = json.loads(line)
break
except ValueError:
# Not yet a complete JSON value
line += next(f)
user_jsons.append(jfile)
现在您有一个包含 json 个对象的列表...[7] 被截断了
In [7]: user_jsons[0]
Out[7]: {u'blocked_by': False,
u'blocking': False,
u'contributors_enabled': False,
u'created_at': u'Thu Jan 30 18:33:13 +0000 2014',
...
In [8]: user_jsons[0]['screen_name']
Out[8]: u'some_users_handle'
您可能会发现 ipython 笔记本 here 是非常有用的资源,尤其是第 9 章。