研究室‎ > ‎卒業生のみなさまへ‎ > ‎松本 宏‎ > ‎python‎ > ‎100本ノック‎ > ‎

第2セット

第2セット部の冒頭に

標準入力からテキスト(tweets.txt.gz)を読み込み,以下の処理を行うプログラムを実装せよ.
とあり、このtweets.txt.gzについては
tweets.txt.gz:日本語のツイートをクロールしたもの(非公開)
と書かれている。
ので、tweetを集めてみようと試みた。

まず、python-twitterモジュールをつかってオープンのツイートを取得してみる。

#! /usr/binh/env python
#-*- coding:utf-8 -*-
#ref: http://code.google.com/p/python-twitter/
import twitter

api = twitter.Api()
statuses = api.GetPublicTimeline()
print [s.user.name for s in statuses]

実行すると、

Traceback (most recent call last):
  File "GetTweets2.py", line 6, in <module>
    statuses = api.GetPublicTimeline()
  File "/usr/lib/python2.6/site-packages/twitter.py", line 1321, in GetPublicTimeline
    json = self._FetchUrl(url,  parameters=parameters)
  File "/usr/lib/python2.6/site-packages/twitter.py", line 2032, in _FetchUrl
    url_data = opener.open(url, encoded_post_data).read()
  File "/usr/lib64/python2.6/urllib2.py", line 397, in open
    response = meth(req, response)
  File "/usr/lib64/python2.6/urllib2.py", line 510, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python2.6/urllib2.py", line 435, in error
    return self._call_chain(*args)
  File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.6/urllib2.py", line 518, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

  • Twitter deprecated many portions of their API as Paul noted. You should check to see if you're using a currently supported mechanism.

  • The official twitter python docs are beyond bad. Last I checked, they did not reflect any current libraries or connection protocols. Use them as a last resort.

  • The twitter recommended python libraries are all grossly out of date. Many don't work at all, most don't work against the current API.

  • まぁ、Twitterさんはあまりpythonのモジュールを更新してくれてないみたいですね。
    よって別のモジュールtweepyをつかってみた

    #! /usr/bin/env python
    # -*- encode: utf-8 -*-
    #ref: http://packages.python.org/tweepy/html/getting_started.html
    import tweepy

    public_tweets = tweepy.api.public_timeline()

    for tweets in public_tweets:
        print tweet.text

    これも

    Traceback (most recent call last):
      File "GetTweets.py", line 6, in <module>
        public_tweets = tweepy.api.public_timeline()
    AttributeError: 'API' object has no attribute 'public_timeline'

    とエラーをはかれたので検索をかけてみた。
    Linuxとかをつついてみるブログ
    によると
    最近(2012年10月末頃?)twitter APIの仕様が変わったから、tweepyからもエラー
    ・tweepyの最新の1.9でもtweepy.api.public_timeline()はダメかも
    ・次のバージョンのtweepyで対応すると思う
    という感じらしい。適当に訳しただけなので当たっているかは不明。
    とある。

    ・・・

    ということで、ツイート取得は断念。

    別の代用テキストデータにはなにがいいかを思案中。。。ということで

    Comments