【问题标题】:How do I get an individual hashtag from the twitter API? (using json in javascript)如何从 twitter API 获取单个主题标签? (在javascript中使用json)
【发布时间】:2017-05-09 22:43:36
【问题描述】:

所以我使用带有节点 js 的 npm 'twit' 来访问 twitter API。我想要的是能够获取特定用户在他们最后几条推文中使用的每个主题标签。

我可以使用以下方法将主题标签视为数组的一部分:

var Twit = require('twit');

var T = new Twit({
  //contains keys
});

var params = { 
  screen_name: '...', 
  count: 10,
}

T.get('statuses/user_timeline', params, gotData); 

function gotData(err, data, response) {
  for (var i = 0; i < data.length; i++) {
    var tweet = data[i].text;
    console.log(data[i].entities.hashtags);
  }
}

由此我得到每条推文的推文数组的输出,例如

[{text:'hashtag',indices:[10, 8]}]

我试过用:

data[i].entities.hashtags[0].text

有多种变体,但似乎从来没有奏效。

任何帮助表示赞赏:)

【问题讨论】:

标签: javascript arrays json node.js twitter


【解决方案1】:

我修改了 twitter 开发页面上的示例 sn-p,以包含您在问题中提到的主题标签对象。您似乎走在正确的轨道上,因此如果没有您的问题的有效示例,我不确定出了什么问题。当一些标签为空时,也许您正试图访问每组标签上的第 0 个索引?

以下是我使用嵌套 for 循环记录标签文本的方式:

for (var i = 0; i < example_data.length; i++) {
  console.log('Hashtags array: ', example_data[i].entities.hashtags); // hashtags is an Array < {text:<string>,indices:Array<number>} >
  var current_hashtags = example_data[i].entities.hashtags;
  for (var j = 0; j < current_hashtags.length; j++) {
    console.log('Hashtag: ', current_hashtags[j].text);
  }
}

您也可以使用地图(或第三方库)来提取文本,例如:

var tags = example_data.map(tweet => tweet.entities.hashtags.map(hashtag => hashtag.text));

var tags = example_data.map((tweet) => {
  return tweet.entities.hashtags.map((hashtag) => {
    return hashtag.text;
  });
});

下面是一个完整的工作小提琴。

var example_data = [{
  "coordinates": null,
  "favorited": false,
  "truncated": false,
  "created_at": "Wed Aug 29 17:12:58 +0000 2012",
  "id_str": "240859602684612608",
  "entities": {
    "urls": [{
      "expanded_url": "/blog/twitter-certified-products",
      "url": "s://t/MjJ8xAnT",
      "indices": [
        52,
        73
      ],
      "display_url": "dev.twitter.com/blog/twitter-c\u2026"
    }],
    "hashtags": [{
      text: 'hashtag',
      indices: [10, 8]
    }],
    "user_mentions": [

    ]
  },
  "in_reply_to_user_id_str": null,
  "contributors": null,
  "text": "Introducing the Twitter Certified Products Program: https://t/MjJ8xAnT",
  "retweet_count": 121,
  "in_reply_to_status_id_str": null,
  "id": 240859602684612608,
  "geo": null,
  "retweeted": false,
  "possibly_sensitive": false,
  "in_reply_to_user_id": null,
  "place": null,
  "user": {
    "profile_sidebar_fill_color": "DDEEF6",
    "profile_sidebar_border_color": "C0DEED",
    "profile_background_tile": false,
    "name": "Twitter API",
    "profile_image_url": "://a0/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
    "created_at": "Wed May 23 06:01:13 +0000 2007",
    "location": "San Francisco, CA",
    "follow_request_sent": false,
    "profile_link_color": "0084B4",
    "is_translator": false,
    "id_str": "6253282",
    "entities": {
      "url": {
        "urls": [{
          "expanded_url": null,
          "url": "",
          "indices": [
            0,
            22
          ]
        }]
      },
      "description": {
        "urls": [

        ]
      }
    },
    "default_profile": true,
    "contributors_enabled": true,
    "favourites_count": 24,
    "url": "",
    "profile_image_url_https": "s://si0/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
    "utc_offset": -28800,
    "id": 6253282,
    "profile_use_background_image": true,
    "listed_count": 10775,
    "profile_text_color": "333333",
    "lang": "en",
    "followers_count": 1212864,
    "protected": false,
    "notifications": null,
    "profile_background_image_url_https": "s://si0/images/themes/theme1/bg.png",
    "profile_background_color": "C0DEED",
    "verified": true,
    "geo_enabled": true,
    "time_zone": "Pacific Time (US & Canada)",
    "description": "The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
    "default_profile_image": false,
    "profile_background_image_url": "://a0/images/themes/theme1/bg.png",
    "statuses_count": 3333,
    "friends_count": 31,
    "following": null,
    "show_all_inline_media": false,
    "screen_name": "twitterapi"
  },
  "in_reply_to_screen_name": null,
  "source": "YoruFukurou",
  "in_reply_to_status_id": null
}, {
  "coordinates": null,
  "favorited": false,
  "truncated": false,
  "created_at": "Sat Aug 25 17:26:51 +0000 2012",
  "id_str": "239413543487819778",
  "entities": {
    "urls": [{
      "expanded_url": "/issues/485",
      "url": "s://t/p5bOzH0k",
      "indices": [
        97,
        118
      ],
      "display_url": "dev.twitter.com/issues/485"
    }],
    "hashtags": [

    ],
    "user_mentions": [

    ]
  },
  "in_reply_to_user_id_str": null,
  "contributors": null,
  "text": "We are working to resolve issues with application management & logging in to the dev portal: https://t/p5bOzH0k ^TS",
  "retweet_count": 105,
  "in_reply_to_status_id_str": null,
  "id": 239413543487819778,
  "geo": null,
  "retweeted": false,
  "possibly_sensitive": false,
  "in_reply_to_user_id": null,
  "place": null,
  "user": {
    "profile_sidebar_fill_color": "DDEEF6",
    "profile_sidebar_border_color": "C0DEED",
    "profile_background_tile": false,
    "name": "Twitter API",
    "profile_image_url": "://a0/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
    "created_at": "Wed May 23 06:01:13 +0000 2007",
    "location": "San Francisco, CA",
    "follow_request_sent": false,
    "profile_link_color": "0084B4",
    "is_translator": false,
    "id_str": "6253282",
    "entities": {
      "url": {
        "urls": [{
          "expanded_url": null,
          "url": "",
          "indices": [
            0,
            22
          ]
        }]
      },
      "description": {
        "urls": [

        ]
      }
    },
    "default_profile": true,
    "contributors_enabled": true,
    "favourites_count": 24,
    "url": "",
    "profile_image_url_https": "s://si0/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
    "utc_offset": -28800,
    "id": 6253282,
    "profile_use_background_image": true,
    "listed_count": 10775,
    "profile_text_color": "333333",
    "lang": "en",
    "followers_count": 1212864,
    "protected": false,
    "notifications": null,
    "profile_background_image_url_https": "s://si0/images/themes/theme1/bg.png",
    "profile_background_color": "C0DEED",
    "verified": true,
    "geo_enabled": true,
    "time_zone": "Pacific Time (US & Canada)",
    "description": "The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
    "default_profile_image": false,
    "profile_background_image_url": "://a0/images/themes/theme1/bg.png",
    "statuses_count": 3333,
    "friends_count": 31,
    "following": null,
    "show_all_inline_media": false,
    "screen_name": "twitterapi"
  },
  "in_reply_to_screen_name": null,
  "source": "YoruFukurou",
  "in_reply_to_status_id": null
}];



example_data.map(tweet => tweet.entities.hashtags.map(hashtag => hashtag.text));

for (var i = 0; i < example_data.length; i++) {
  console.log('Hashtags array: ', example_data[i].entities.hashtags); // hashtags is an Array < {text:<string>,indices:Array<number>} >
  var current_hashtags = example_data[i].entities.hashtags;
  for (var j = 0; j < current_hashtags.length; j++) {
    console.log('Hashtag: ', current_hashtags[j].text);
  }
}

【讨论】:

  • 太棒了!!非常感谢,仍然不确定我之前哪里出错了,但现在它工作得很好,所以非常感谢:)
猜你喜欢
  • 2015-05-15
  • 2013-12-10
  • 1970-01-01
  • 1970-01-01
  • 2012-12-03
  • 2011-10-30
  • 2012-08-15
  • 2020-11-17
  • 1970-01-01
相关资源
最近更新 更多