在Python中,如何阅读Twitter消息并创建.JSON文件

时间:2015-06-16 10:43:29

标签: python json twitter

有些时候我发现了一个Python脚本来导入Twitter消息并将它们写入.JSON文件。问题是根据我的JSON查看器,这不是有效的JSON格式。脚本是:

import twitter

twitter_api = twitter.Twitter(auth=auth)
q = "question"
count = 200


search_results = twitter_api.search.tweets(q=q, count=count, lang="nl")
statuses = search_results['statuses']


for _ in range(25): 
    try:
        next_results = search_results['search_metadata']['next_results']
    except KeyError, e: # No more results when next_results doesn't exist
        break

    kwargs = dict([ kv.split('=') for kv in next_results[1:].split("&") ]) # Create a dictionary from the query string params
    search_results = twitter_api.search.tweets(**kwargs)
    statuses += search_results['statuses']

import io
import json
import twitter


QUERY = 'question'

# The file to write output as newline-delimited JSON documents
OUT_FILE = question + "5.json"


# Write one tweet per line as a JSON document. 

with io.open(OUT_FILE, 'w', encoding='utf-8', buffering=1) as f:
    for tweet in statuses:
        f.write(unicode(u'{0}\n'.format(json.dumps(tweet, ensure_ascii=False))))
        print tweet['text']

第一条推文信息如下所示:

  

[{u'contributors':没有,你被'截断':假,你''''你'@ ABNAMRO Bedankt   voor de goede en snelle服务!   !',u'is_quote_status':False,u'in_reply_to_status_id':无,你'':610448080702439424L,u'favorite_count':0,你的来源':u'Twitter for   Android的,u'retweeted ':虚假,u'coordinates':无,u'entities ':{u'symbols':[],u'user_mentions ':[{u'id':2270841,u'indices': [0,8],u'id_str ':u'2270841',u'screen_name ':u'ABNAMRO',u'name':u'ABN   AMRO '}],u'hashtags':[],u'urls ':[]},u'in_reply_to_screen_name':u'ABNAMRO 'u'in_reply_to_user_id ':2270841,u'retweet_count':0,u'id_str' :u'610448080702439424' ,u'favorited ':虚假,u'user':{u'follow_request_sent ':虚假,u'profile_use_background_image':真,u'default_profile_image ':虚假,u'id':130104974,u'profile_background_image_url_https ':U'https://abs.twimg.com/images/themes/theme6/bg.gif 'u'verified ':虚假,u'profile_text_color':u'333333' ,u'profile_image_url_https ':U'https://pbs.twimg.com/profile_images/457174881499889665/IkudfAL6_normal.jpeg',u'profile_sidebar_fill_color':u'A0C5C7 ”,u'entities ':{u'description':{u'urls ':[]}},u'followers_count':15,u'profile_sidebar_border_color ':u'86A4A6',u'id_str ':u'130104974' ,u'profile_background_color ':u'709397',u'listed_count ':0,u'is_translation_enabled':虚假,u'utc_offset ':10800,u'statuses_count':37,u'description':u'Trotse   moeder van 2   dochters 'u'friends_count ':52,u'location':u'Delft',u'profile_link_color ':u'FF3300',u'profile_image_url ':U'http://pbs.twimg.com/profile_images/457174881499889665/IkudfAL6_normal.jpeg 'u'following':假,u'geo_enabled ':虚假,u'profile_background_image_url':U 'http://abs.twimg.com/images/themes/theme6/bg.gif',u'screen_name ':u'Deborah_81',u'lang ':u'nl',u'profile_background_tile“:假的, u'favourites_count ':2,u'name':u'Deborah 'u'notifications ':虚假,u'url':无,u'created_at':u'Tue   4月06日09:37:51 +0000   2010' ,u'contributors_enabled ':虚假,u'time_zone':u'Athens',u'protected ':虚假,u'default_profile':虚假,u'is_translator ':假},u'geo':无,U 'in_reply_to_user_id_str':u'2270841' ,u'lang ':u'nl',u'created_at“:u'Mon   6月15日14:05:38 +0000   2015' ,u'in_reply_to_status_id_str ':无,u'place':无,u'metadata ':{u'iso_language_code':u'nl”,u'result_type ':u'recent'}}]

如何将消息格式化为正确显示为JSON?

1 个答案:

答案 0 :(得分:0)

我看到的问题是,您在将数据写入json格式的文件之前对数据进行编码,这会给您带来错误。这应该解决它:

{{1}}

在此SO问题以及Mining the Social Web, 2nd Edition

中提到过