2011年11月28日月曜日

[Python][Mining the Social Web]Twitter APIを使用してデータを集める

Twitter APIを使用するためのPythonパッケージを追加し、実際にTwitterデータにアクセスしてみる。

$ sudo easy_install twitter
Searching for twitter
Reading http://pypi.python.org/simple/twitter/
Reading http://mike.verdone.ca/twitter/
Best match: twitter 1.7.2
Downloading http://pypi.python.org/packages/source/t/twitter/twitter-1.7.2.tar.gz#md5=a81b512cc8be5ba278bdaceb6d988fe6
Processing twitter-1.7.2.tar.gz
Running twitter-1.7.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-1iZuPg/twitter-1.7.2/egg-dist-tmp-7cd07T
Adding twitter 1.7.2 to easy-install.pth file
Installing twitter-stream-example script to /usr/local/bin
Installing twitter script to /usr/local/bin
Installing twitterbot script to /usr/local/bin
Installing twitter-log script to /usr/local/bin


Installed /Library/Python/2.7/site-packages/twitter-1.7.2-py2.7.egg
Processing dependencies for twitter
Finished processing dependencies for twitter


パッケージドキュメントを読むためにはpydocを実行する。
*nix : pydoc twitter.Twitter
(Windows : python -mpydoc twitter.Twitter)

TwitterのトレンドAPIを実行してみるが、エラー発生。
$ python
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import twitter
>>> twitter_api = twitter.Twitter(domain="api.twitter.com", api_version='1')
>>> trends = twitter_api.trends()
Traceback (most recent call last):
File "", line 1, in
File "build/bdist.macosx-10.7-intel/egg/twitter/api.py", line 165, in __call__
File "build/bdist.macosx-10.7-intel/egg/twitter/api.py", line 180, in _handle_response
twitter.api.TwitterHTTPError: Twitter sent status 404 for URL: 1/trends.json using parameters: ()
details:

はて、404エラー出てるな。
とりあえず、本文に記載のURL:http://search.twitter.com/1/trends.jsonを入力してみるがこちらもエラー。
よく見ると、このURLってhttp指定してるけど、Twitterってhttps使うように変わったような...

と、いうことでTwitter API のドキュメントを確認してみる。
https://dev.twitter.com/docs/api/1/get/trends/%3Awoeid

やっぱり、API仕様変わってますね。ということで、モジュール修正が必要だな。

0 件のコメント:

コメントを投稿