简体   繁体   English

从json提取推文

[英]extracting tweets from json

I used the following code to get a single tweet: 我使用以下代码获得了一条推文:

import twitter
import tweepy
import json
CONSUMER_KEY = ""
CONSUMER_SECRET=""
OAUTH_TOKEN=""
OAUTH_TOKEN_SECRET=""

twitter_api = twitter.Api(CONSUMER_KEY,
                  CONSUMER_SECRET,
                  OAUTH_TOKEN,
                 OAUTH_TOKEN_SECRET)


auth = tweepy.auth.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter_api1 = tweepy.API(auth, parser=tweepy.parsers.JSONParser())

search_results = twitter_api1.search(q="Movies", count=1)
statuses = search_results['statuses']
print(json.dumps(statuses[0], indent =1)).

This code gives the following JSON file: 此代码提供以下JSON文件:

{
 "created_at": "Wed Aug 02 18:21:35 +0000 2017",
 "id": 892812639314817033,
 "id_str": "892812639314817033",
 "text": "\"The Best TV Shows and Movies New to Netflix, Amazon and More in August\" by MONICA CASTILLO via NYT ",
 "truncated": false,
 "entities": {
 "hashtags": [],
 "symbols": [],
  "user_mentions": [],
  "urls": [
   {
    "url": "",
    "expanded_url": "",
    "display_url": "",
    "indices": [
     100,
     123
    ]
   }
  ]
 },
 "metadata": {
  "iso_language_code": "en",
  "result_type": "recent"
 },
  "source": "<a href=\"https://ifttt.com\" rel=\"nofollow\">IFTTT</a>",
  "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": 39630578,
  "id_str": "39630578",
  "name": "TV Mogul",
  "screen_name": "TheTVStation",
  "location": "Behind the Remote Control",
  "description": "So many shows..so many networks....when is your favorite 
time to watch? Who is your favorite news or personality at your station..down the street from The Rink",
  "url": null,
  "entities": {
   "description": {
    "urls": []
   }
}
.
.
. AND SO On

Please tell me how i can extract the "text" field from this JSON file. 请告诉我如何从此JSON文件中提取“文本”字段。 I tried using statuses[0]['text'] but it gives an error saying: 我尝试使用statuss [0] ['text'],但显示错误消息:

TypeError: list indices must be integers or slices, not str TypeError:列表索引必须是整数或切片,而不是str

Please help me out. 请帮帮我。

use 采用

statuses[0].text 状态[0]的.text

that will give you the value 那会给你价值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM