質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

1回答

2227閲覧

Node.jsでtwitterタイムラインをJSONに書き出し

nano-dia

総合スコア32

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2016/01/10 06:09

編集2016/01/11 05:28

コンソールでは

C:\Users\Desktop>node test.js @TwitterJP [ { created_at: 'Fri Jan 08 09:28:51 +0000 2016', id: 685392711499661300, id_str: '685392711499661312', text: 'TwitterJPからの #クリスマスボックス の当選者の皆さん。お待たせしまし た。本日プレゼントを配送いたしましたので、数日中にお手元に届くと思われます。著名 人の方々からのプレゼントの当選者の皆さんはもう少しお待ちください。\nhttps://t.co /h2b5y2NqFP', source: '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>' , 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: 7080152, id_str: '7080152', name: 'TwitterJP', screen_name: 'TwitterJP', location: '東京都中央区', description: '日本語版Twitter公式アカウントです。サービスに関しては https ://t.co/mfQkUQLUhe をご参照ください。', url: 'https://t.co/A9dNuL0CCa', entities: [Object], protected: false, followers_count: 2090430, friends_count: 114, listed_count: 16482, created_at: 'Tue Jun 26 01:54:35 +0000 2007', favourites_count: 108, utc_offset: 32400, time_zone: 'Tokyo', geo_enabled: false, verified: true, statuses_count: 3587, lang: 'ja', contributors_enabled: false, is_translator: false, is_translation_enabled: false, profile_background_color: 'C0DEED', profile_background_image_url: 'http://pbs.twimg.com/profile_background_im ages/567482719571959809/WwgdnJCP.jpeg', profile_background_image_url_https: 'https://pbs.twimg.com/profile_backgr ound_images/567482719571959809/WwgdnJCP.jpeg', profile_background_tile: true, profile_image_url: 'http://pbs.twimg.com/profile_images/3407356865/62f0d5 3222361fbd2c1fe9889f4cc559_normal.png', profile_image_url_https: 'https://pbs.twimg.com/profile_images/3407356865 /62f0d53222361fbd2c1fe9889f4cc559_normal.png', profile_banner_url: 'https://pbs.twimg.com/profile_banners/7080152/145186 4421', profile_link_color: '0084B4', profile_sidebar_border_color: 'FFFFFF', profile_sidebar_fill_color: 'DDEEF6', profile_text_color: '333333', profile_use_background_image: true, has_extended_profile: true, default_profile: false, default_profile_image: false, following: false, follow_request_sent: false, notifications: false }, geo: null, coordinates: null, place: null, contributors: null, quoted_status_id: 679875462294716400, quoted_status_id_str: '679875462294716416', quoted_status: { created_at: 'Thu Dec 24 04:05:16 +0000 2015', id: 679875462294716400, id_str: '679875462294716416', text: '最後の� #クリスマスボックス は感謝を込めて @TwitterJP から!投票し てくださった方から抽選で各5名様に。 応募規約: https://t.co/N4tBi2HcDv https://t .co/r7HZ0PaMVN \nhttps://t.co/r7HZ0PaMVN', source: '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</ a>', 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: [Object], geo: null, coordinates: null, place: null, contributors: null, is_quote_status: false, retweet_count: 246, favorite_count: 301, entities: [Object], favorited: false, retweeted: false, possibly_sensitive: false, lang: 'ja' }, is_quote_status: true, retweet_count: 16, favorite_count: 37, entities: { hashtags: [Object], symbols: [], user_mentions: [], urls: [Object] }, favorited: false, retweeted: false, possibly_sensitive: false, lang: 'ja' } ]

うまくタイムラインを取得できるのですが
fsモジュールを使って,JSON形式に保存しようとすると,

[object Object]

となってしまいます.どうすればコンソールのように出力できるのでしょうか.

###Node.jsプログラム

var params = {screen_name: 'TwitterJP',count:1}; console.log("@"+params.screen_name); T.get('statuses/user_timeline', params, function(error, tweets){ if (!error) { console.log(tweets); fs.appendFileSync('twitter1.txt',tweets + "\n",'utf-8'); } });

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

thom.jp

2016/01/11 04:56

タイトルがあんまりです。
nano-dia

2016/01/11 05:10

タイトルを元に戻しました
guest

回答1

0

自己解決

ファイル書き込み時にJSON形式に変換指定すれば解決しました.

fs.appendFileSync('twitter1.json',JSON.stringify(tweets)+"\n",'utf-8');

投稿2016/01/11 03:39

編集2016/01/11 03:45
nano-dia

総合スコア32

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問