【问题标题】:Python: Reading Tweets from file, no user infoPython:从文件中读取推文,没有用户信息
【发布时间】:2018-02-20 18:06:50
【问题描述】:

我是 python 新手,正在玩推文。

这可能很愚蠢,但我遇到了关于 tweets 用户实体的问题。好像我正在将 .json 文件读取到 dict 中。我可以在字典中的不同位置打印出推文,例如: 推文 = 推文 [7] 打印(推文)。

code 1

当我打印 print (tweet['entities']) 时,我会看到主题标签之类的东西。 当我打印用户密钥时,它们会显示出来。

code 2

但是,问题是当我尝试遍历并打印出用户内容的内容时,例如屏幕名称、位置等

code3

statuses = []
for tweet in tweets:
    statuses.append(tweet['text']) 

ids = []
for tweet in tweets:
    ids.append(tweet['id_str'])

times = []
for tweet in tweets:
    times.append(tweet['user']['created_at'])

screen_names = []
for tweet in tweets:
    screen_names.append (tweet['user']['screen_name'])

读取json有问题吗?这是文件https://drive.google.com/file/d/1u2soBW4PnRTuCMLdKODf4oKl9Zz88D7l/view?usp=sharing中推文的链接 我只是愚蠢并做了一些可笑的错误吗?非常感谢帮助新手。我环顾四周,找不到有关用户资料不存在的任何信息。抱歉,如果我没有提供帮助所需的所有信息。

代码:

    import json

    import tweepy

    import twitter

    tweets = []

    for line in open('tweets5.json'):

      try: 
        tweets.append(json.loads(line))

    except:
        pass

    print (len(tweets))

    tweet = tweets[2]

    print (tweet)

    print (tweet['entities'])

    print (tweet['user'].keys())

    print (tweet['text'])

    print (tweet['id_str'])

    print (tweet['user']['name'])

    print (tweet['user']['location'])
    print (tweet['place'])
    print(tweet['user']['screen_name'])
    print (tweet['geo'])
    print(tweet['user']['created_at'])

    print (tweet['favorite_count'])
    print (tweet['retweet_count'])


 **//errors start here I get a key error for id_str when I try to do IDs, so I tried moving things around to see if any of them would go through successfully & move on, but I get a key error for whatever is in the brackets first.

statuses = []
for tweet in tweets:
    statuses.append(tweet['text']) 

ids = []
for tweet in tweets:
    ids.append(tweet['id_str'])

times = []
for tweet in tweets:
    times.append(tweet['user']['created_at'])

screen_names = []
for tweet in tweets:
    screen_names.append (tweet['user']['screen_name']) 

这就是推文的样子:

{"created_at":"Mon Feb 19 04:08:20 +0000 2018","id":965437872395472896,"id_str":"965437872395472896","text":"RT @IronStache: .@SpeakerRyan spent all weekend in Florida, but he wouldn\u2019t even visit Parkland to offer comfort and support. \n\nPaul Ryan w\u2026","source":"\u003ca href=\"https:\/\/mobile.twitter.com\" rel=\"nofollow\"\u003eTwitter Lite\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":2510706061,"id_str":"2510706061","name":"Bill McClure","screen_name":"RealBillMcClure","location":"Boca Raton, FL","url":null,"description":"Buck & El's kid, husband, dad, friend of the show. Everything I ever needed to know was learned on the streets of Youngstown, OHIO.","translator_type":"none","protected":false,"verified":false,"followers_count":576,"friends_count":1200,"listed_count":5,"favourites_count":2324,"statuses_count":5541,"created_at":"Tue May 20 15:28:42 +0000 2014","utc_offset":null,"time_zone":null,"geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"3B94D9","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/864933232118312960\/kqhxw1LB_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/864933232118312960\/kqhxw1LB_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/2510706061\/1504194378","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Mon Feb 19 00:45:19 +0000 2018","id":965386782471872512,"id_str":"965386782471872512","text":".@SpeakerRyan spent all weekend in Florida, but he wouldn\u2019t even visit Parkland to offer comfort and support. \n\nPau\u2026 https:\/\/t.co\/t2H4QGJ0s7","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":true,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":292083207,"id_str":"292083207","name":"Randy Bryce","screen_name":"IronStache","location":"Caledonia, WI","url":"http:\/\/www.randybryceforcongress.com","description":"Democrat for WI-01. Father, Army veteran, Ironworker, Cancer survivor, Wisconsin4ever. Most tweets mine.","translator_type":"none","protected":false,"verified":true,"followers_count":234147,"friends_count":4069,"listed_count":1130,"favourites_count":21552,"statuses_count":26855,"created_at":"Tue May 03 02:27:46 +0000 2011","utc_offset":-21600,"time_zone":"Central Time (US & Canada)","geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/667813427\/181a97f32cdd68a3fb6a8e744a68a1b7.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/667813427\/181a97f32cdd68a3fb6a8e744a68a1b7.png","profile_background_tile":true,"profile_link_color":"1B95E0","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/878715837259153410\/LQ0QNz2y_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/878715837259153410\/LQ0QNz2y_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/292083207\/1497557512","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"extended_tweet":{"full_text":".@SpeakerRyan spent all weekend in Florida, but he wouldn\u2019t even visit Parkland to offer comfort and support. \n\nPaul Ryan won\u2019t do anything to prevent future attacks. We have to replace him with a Congressperson who will. https:\/\/t.co\/PBFoQ6IHfv","display_text_range":[0,245],"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/PBFoQ6IHfv","expanded_url":"http:\/\/www.palmbeachpost.com\/news\/trump-palm-beach-mar-lago-meeting-with-house-speaker-paul-ryan\/QisKep1LXdV5zQRZWQ5OLO\/","display_url":"palmbeachpost.com\/news\/trump-pal\u2026","indices":[222,245]}],"user_mentions":[{"screen_name":"SpeakerRyan","name":"Paul Ryan","id":18916432,"id_str":"18916432","indices":[1,13]}],"symbols":[]}},"quote_count":191,"reply_count":379,"retweet_count":4375,"favorite_count":11011,"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/t2H4QGJ0s7","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/965386782471872512","display_url":"twitter.com\/i\/web\/status\/9\u2026","indices":[117,140]}],"user_mentions":[{"screen_name":"SpeakerRyan","name":"Paul Ryan","id":18916432,"id_str":"18916432","indices":[1,13]}],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"IronStache","name":"Randy Bryce","id":292083207,"id_str":"292083207","indices":[3,14]},{"screen_name":"SpeakerRyan","name":"Paul Ryan","id":18916432,"id_str":"18916432","indices":[17,29]}],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1519013300404"}

【问题讨论】:

  • edit您的回答将您的代码作为文本包含在您的问题中,而不是作为图像。谢谢:)
  • 完成!抱歉,这是我的第一个问题。

标签: python json twitter


【解决方案1】:

我认为这条线

print (['user']['screen_name'])

应该是:

print(tweet['user']['screen_name'])

【讨论】:

  • 谢谢,我仍然遇到同样的错误。 KeyError:'用户'
  • 你能显示print(tweets)的完整输出吗?我可能误解了您的数据结构。如果太大而无法全部显示,只需显示前几条推文即可。
  • 我要打印这个: print (tweet['text']) print (tweet['id_str']) print (tweet['user']['name']) print (tweet[ '用户']['位置'])
  • json 文件中的推文很大,我不能在这里粘贴 :( 这是 google 驱动器上 txt 的链接:drive.google.com/file/d/1u2soBW4PnRTuCMLdKODf4oKl9Zz88D7l/…
  • 它也不喜欢推文中的推文部分,说语法无效
猜你喜欢
  • 2017-01-11
  • 2022-11-01
  • 2019-02-28
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 2014-05-27
相关资源
最近更新 更多