Twitter APIでHadoop用のデータ取得
Twitter APIでHadoop用のデータを取得してみました。一回程度だと傾向がまったく不明なので(よく考えたら当たり前ですね)、定期的に取得して分析用のデーットしてどうなのか見てみたいと思います。【環境】・MAC OS X 10.11.6・$python -V Python 3.5.2 :: Anaconda 4.1.1 (x86_64) (データ取得プログラムとして使用)【やりたいこと】 ・Hadoopで分析するためのデータをTwitterから取得する。 ①自分のタイムラインのデータ取得 ②キーワードで検索したデータ取得【実現方法】1. Twitter社がデータ取得用のAPIを公開しているので、APIに対応したプログラムを 自分のPC上で実行し、データを取得する。2. APIは以下に公開されていて、①GET statuses/mentions_timelineや②The Search API を活用します。 https://dev.twitter.com/overview/documentation https://dev.twitter.com/rest/reference/get/statuses/mentions_timeline3. 以下の Webを参考にさせていただきました。 ①タイムライン表示 http://qiita.com/yubais/items/dd143fe608ccad8e9f85 http://qiita.com/konojunya/items/59a68d35e44db8b87186 ②全文検索 http://www.jiriki.co.jp/blog/python/python-twitter http://qiita.com/mima_ita/items/ba59a18440790b12d97e【結論・分かったこと】①データ自体は入手でき、HaoopでWordのカウントも可能。 ただし、センチメント分析として、どのワードに着目するかは人間が考える必要がある。 word自体はただの単語に過ぎない。②日本語の文章においては空白がないため、Hadoopのwordカウント機能があまり有効でない。 文章自体が一つのwordになってしまう(もう少し研究の余地あり)。以下は三菱東京UFJ銀行のデータをツイッターで取得しHadoopでword countしたデータをカウントの多い順でsortした例です。【手順】1. Twitter社にて登録。 以下の4つを取得 ①Consumer Key: ②Consumer Secret: ③Access Token: ④Access Token Secret:2. データ取得時に必要な認証pythonのライブラリをインストール ①GitHubに公開されている「requests-oauthlib」ライブラリの「OAuth1Session」機能を使用する。 https://github.com/requests/requests-oauthlib ②MACのPIPコマンドで「requests-oauthlib」ライブラリをインストール。 $ pip install requests requests_oauthlib3. python-twitterのライブラリをインストール takeshi$pip install python-twitter Collecting python-twitter Downloading python_twitter-3.1-py3-none-any.whl (69kB) 100% |████████████████████████████████| 71kB 710kB/s Collecting future (from python-twitter) Downloading future-0.15.2.tar.gz (1.6MB) 100% |████████████████████████████████| 1.6MB 323kB/s Requirement already satisfied (use --upgrade to upgrade): requests in /anaconda/lib/ python3.5/site-packages (from python-twitter)Requirement already satisfied (use --upgrade to upgrade): requests-oauthlib in /anaconda/lib/python3.5/site-packages (from python-twitter)Requirement already satisfied (use --upgrade to upgrade): oauthlib>=0.6.2 in /anaconda/lib/python3.5/site-packages (from requests-oauthlib->python-twitter)Building wheels for collected packages: future Running setup.py bdist_wheel for future ... done Stored in directory: /Users/takeshi/Library/Caches/pip/wheels/11/c5/d2/a d287de27d0f0d646f119dcffb921f4e63df128f28ab0a1bda Successfully built future Installing collected packages: future, python-twitter Successfully installed future-0.15.2 python-twitter-3.1 takeshi$4. Test用のプログラム①タイムライン表示# -*- coding: utf-8 -*-import jsonfrom requests_oauthlib import OAuth1SessionCK = ‘xxxx’ # Consumer KeyCS = ‘xxxx’ # Consumer SecretAT = ‘xxxx’ # Access TokenAS = ‘xxxx’ # Accesss Token Secert# タイムライン取得用のURLurl = "https://api.twitter.com/1.1/statuses/home_timeline.json"# とくにパラメータは無いparams = {}# OAuth で GETtwitter = OAuth1Session(CK, CS, AT, AS)req = twitter.get(url, params = params)if req.status_code == 200: # レスポンスはJSON形式なので parse する timeline = json.loads(req.text) # 各ツイートの本文を表示 for tweet in timeline: print(tweet["text"])else: # エラーの場合 print ("Error: %d" % req.status_code)—— EOF —②全文検索 *便宜上、三菱東京UFJをキーワードにしています#!/usr/bin/env python# coding: utf-8import twitterCONSUMER_KEY = “xxxx”CONSUMER_SECRET = "xxxx"ACCESS_TOKEN_KEY = "xxxx"ACCESS_TOKEN_SECRET = "xxxx"api = twitter.Api(consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET, access_token_key=ACCESS_TOKEN_KEY, access_token_secret=ACCESS_TOKEN_SECRET)tweets = api.GetSearch(term=u"三菱東京UFJ", count=100)for tweet in tweets: print(tweet.text)—— EOF — (1)Pythonでのforループについて L = [ 'Toyota', 'Nissan', 'Honda' ] # リストを作成 for x in L : print x (2)MAC上でテキストファイルをエクセルで取り込み時にエラーになる場合は 一旦、テキストファイルを別名で保存する。保存する時にUnicode(UTF-16)を選択する。 (3)オプションでCountを200としてあげると、ツイート数が200まで取得できる(20から200まで) req = twitter.get("https://api.twitter.com/1.1/statuses/home_timeline.json?count=200", params = params)5. それぞれのプログラムを実行 ー抜粋ー①タイムラインtakeshi$python timeline.pyRT @ikeay: 今日は油絵の街にきています。 https://t.co/3wYFqk8lnlNetflixがOSSとして公開しているSpinakerさん。Azureとの組み合わせについて解説した資料を発見。これはちゃんと使えたら、とても強力な感じですね。全然情報がないのだが、やってみようかな。https://t.co/6ALY5byfNfディープで震える深淵への誘い:電子回路開発ゲー「SHENZHEN I/O」 | はげあたま.org https://t.co/7m6fs8qiAf https://t.co/I4EJy2GqzS②全文検索takeshi$python twitprint.py三菱東京UFJ銀行行員が不正接待の実態!10年間で約730億円を融資とかリアル半沢直樹の世界かよ! - NAVER まとめ https://t.co/mr1AsD9wE8 #三菱東京UFJ銀行 #三菱東京UFJ #銀行三菱東京UFJ銀行行員が不正接待の実態!10年間で約730億円を融資とかリアル半沢直樹の世界かよ! - NAVER まとめ https://t.co/AFjeAJ4Pzz #三菱東京UFJ銀行 #三菱東京UFJ #銀行三菱東京UFJ銀行行員が不正接待の実態!10年間で約730億円を融資とかリアル半沢直樹の世界かよ! - NAVER まとめ https://t.co/xspTcaHJVJ #三菱東京UFJ銀行 #三菱東京UFJ #銀行三菱UFJ 社員に仮想通貨付与 #三菱東京UFJ #仮想通貨 #カブドットコム証券 https://t.co/Bcr0xzHGqDいまさっき【終わり】