gpt4 book ai didi

json - 如何使用 json.stringify 获取推文的某些参数并将其保存到文件中? (我使用的方法适用于 Stream,但不适用于 GET)

转载 作者:太空宇宙 更新时间:2023-11-03 22:22:33 26 4
gpt4 key购买 nike

我试图仅将用户@screen_name 和使用 Twitter API 获得的推文文本保存到文件中,但在 Twitter API 网站或 json.strinigify 文档上找不到任何相关文档。我正在使用的代码由于某种原因实际上不能与 GET 一起使用,它是这样的(我在 Google 中找不到与解决我的问题相关的任何内容):

var params = {q: '@Avengers, count: 1, include_rts: 1};
client.get('search/tweets', params, function(error, tweets, response) {
if (!error) {
console.log(tweets);
fs.appendFile("tweet.txt", JSON.stringify(tweets.text), function (err) {
//user.name or .text doesn't work in GET and I am trying to find the methods
//that work for GET
if (err) throw err;
console.log('Saved!');
});
}
});

当我使用 Stream 来使用类似的东西时,它确实有效,并且保存了发布推文的用户名和推文的文本(这是我需要的)。我的问题是,流没有任何方法来获取用户@screen_name 的推文,所以我继续使用 GET 方法。我执行此操作的流代码如下:

var stream = client.stream('statuses/filter', {track: 'Avengers'});
stream.on('data', function(event) {
console.log("Tweeted by ::::>>>" + event.user.name + " ::::>>> " + "Tweet is :::>>>> " + event.text + " ::::>>>");
fs.appendFile("tweet.txt", JSON.stringify(event.user.name+' ////////'+event.text)+"\n", function (err) {
if (err) throw err;
console.log('Saved!');
});
});

我使用 GET 方法得到的结果是一大堆参数,当它保存在文件中时我不需要:

 "statuses": [
{
"created_at": "Sun Sep 23 05:03:17 +0000 2018",
"id": 1043727480572522500,
"id_str": "1043727480572522496",
"text": "@Avengers Gamora, Dr Strange and Vision all made comment about dying before given up Stones in 1st 45 minutes. Gett… --------OtAEPXbih4",
"truncated": true,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [
{
"screen_name": "Avengers",
"name": "The Avengers",
"id": 393852070,
"id_str": "393852070",
"indices": [
0,
9
]
}
],
"urls": [
{
"url": "--------OtAEPXbih4",
"expanded_url": "--------i/web/status/1043727480572522496",
"display_url": "twitter.com/i/web/status/1…",
"indices": [
117,
140
]
}
]
},
"metadata": {
"iso_language_code": "en",
"result_type": "recent"
},
"source": "<a href=\"--------#!/download/ipad\" rel=\"nofollow\">Twitter for iPad</a>",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": 393852070,
"in_reply_to_user_id_str": "393852070",
"in_reply_to_screen_name": "Avengers",
"user": {
"id": 1558865406,
"id_str": "1558865406",
"name": "john sabo",
"screen_name": "saybow1969",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 52,
"friends_count": 397,
"listed_count": 5,
"created_at": "Sun Jun 30 20:40:04 +0000 2013",
"favourites_count": 680,
"utc_offset": null,
"time_zone": null,
"geo_enabled": true,
"verified": false,
"statuses_count": 24062,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "C0DEED",
"profile_background_image_url": "----://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_image_url_----": "----://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_tile": false,
"profile_image_url": "----://pbs.twimg.com/profile_images/825511373396439040/zZf_Z9Hu_normal.jpg",
"profile_image_url_----": "----://pbs.twimg.com/profile_images/825511373396439040/zZf_Z9Hu_normal.jpg",
"profile_banner_url": "----://pbs.twimg.com/profile_banners/1558865406/1486454716",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"translator_type": "none"
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 0,
"favorite_count": 0,
"favorited": false,
"retweeted": false,
"lang": "en"
}
],
"search_metadata": {
"completed_in": 0.015,
"max_id": 1043727480572522500,
"max_id_str": "1043727480572522496",
"next_results": "?max_id=1043727480572522495&q=%40Avengers&count=1&include_entities=1",
"query": "%40Avengers",
"refresh_url": "?since_id=1043727480572522496&q=%40Avengers&include_entities=1",
"count": 1,
"since_id": 0,
"since_id_str": "0"
}
}

我的 Stream 方法正确执行此操作,但我的 GET 没有,这就是我的流给我的:

"Trash Flaten ////////@lliejuve @lliejuve on current tv it has to be the voice actor for Thor in Disney's Avengers Assemble. So o respect…

如您所见,Stream 为我提供了我需要的用户 @screen_name 和推文文本,但在 GET 方法中 user.name 和 .text 并没有这样做。

最佳答案

var params = {q: '@Avengers, count: 1, include_rts: 1};
client.get('search/tweets', params, function(error, tweets, response) {
if (!error) {
console.log(tweets);
fs.appendFile("tweet.txt", JSON.stringify(tweets.statuses[0].text), function (err) {
if (err) throw err;
console.log('Saved!');
});
}
});

尝试上面的更改。它应该可以工作。

关于json - 如何使用 json.stringify 获取推文的某些参数并将其保存到文件中? (我使用的方法适用于 Stream,但不适用于 GET),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52463226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com