ふにゃるんv2

もとは、http://d.hatena.ne.jp/Wacky/

easy_installを使って、mechanize for Pythonを試してみる

ネットをぼ〜っと眺めていると、Python版のmechanizeってのがあるそうで。
そういや、自分、以前ブクマしたっけな?


どんなものか、ちょっと試してみる事にします。
念の為、本家ページの説明を…。


…英語なんてわかんないので、機械翻訳にかけちゃう。
要するに、

  • お奨めは、easy_installだよ。
  • だけど、昔の'python setup.py install'方式もサポートしているよ。
  • 後、ClientFormライブラリを使用しているよ。

って事みたいです。


んじゃぁ、easy_installを入れます。
(以前入れたと思ったんですが、入ってませんでした。あれぇ?)


やる事は簡単で、ez_setup.pyってファイルをダウンロードして、実行するだけです。
すると、Scriptsフォルダに、easy_install.exeが出来上がります。

F:\Wacky\Test\python>python ez_setup.py
Downloading http://cheeseshop.python.org/packages/2.4/s/setuptools/setuptools-0.6c1-py2.4.egg
Processing setuptools-0.6c1-py2.4.egg
creating c:\tool2\python24\lib\site-packages\setuptools-0.6c1-py2.4.egg
Extracting setuptools-0.6c1-py2.4.egg to c:\tool2\python24\lib\site-packages
Adding setuptools 0.6c1 to easy-install.pth file
Installing easy_install-script.py script to C:\tool2\Python24\Scripts
Installing easy_install.exe script to C:\tool2\Python24\Scripts
Installing easy_install-2.4-script.py script to C:\tool2\Python24\Scripts
Installing easy_install-2.4.exe script to C:\tool2\Python24\Scripts

Installed c:\tool2\python24\lib\site-packages\setuptools-0.6c1-py2.4.egg
Processing dependencies for setuptools==0.6c1

C:\tool2\Python24\Scripts>dir easy*.*
 ドライブ C のボリューム ラベルがありません。
 ボリューム シリアル番号は 4065-FD4A です

 C:\tool2\Python24\Scripts のディレクトリ

2006/08/06  16:19               310 easy_install-2.4-script.py
2006/08/06  16:19             6,144 easy_install-2.4.exe
2006/08/06  16:19               302 easy_install-script.py
2006/08/06  16:19             6,144 easy_install.exe
               4 個のファイル              12,900 バイト
               0 個のディレクトリ  83,616,264,192 バイトの空き領域


では、mechanizeを入れます。
本家ページの推奨である、'easy_install mechanize'って記述に従ってみます。

F:\Wacky\Test\python>easy_install mechanize
Searching for mechanize
Reading http://www.python.org/pypi/mechanize/
Reading http://wwwsearch.sourceforge.net/mechanize/
Reading http://www.python.org/pypi/mechanize/0.1.2b
Best match: mechanize 0.1.2b
Downloading http://wwwsearch.sourceforge.net/mechanize/src/mechanize-0.1.2b.zip
Processing mechanize-0.1.2b.zip
Running mechanize-0.1.2b\setup.py -q bdist_egg --dist-dir c:\docume~1\xxx\locals~1\temp\easy_install-q1ladt\mechanize-0.1.2b\egg-dist-tmp-6exufd
warning: no files found matching '0.1.0-changes.txt'
Adding mechanize 0.1.2b to easy-install.pth file

Installed c:\tool2\python24\lib\site-packages\mechanize-0.1.2b-py2.4.egg
Processing dependencies for mechanize
Searching for ClientForm==dev,>=0.2.2
Reading http://www.python.org/pypi/ClientForm/
Reading http://wwwsearch.sourceforge.net/ClientForm/
Reading http://www.python.org/pypi/ClientForm/0.2.2
Best match: ClientForm 0.2.2
Downloading http://wwwsearch.sourceforge.net/ClientForm/src/ClientForm-0.2.2.zip
Processing ClientForm-0.2.2.zip
Running ClientForm-0.2.2\setup.py -q bdist_egg --dist-dir c:\docume~1\xxx\locals~1\temp\easy_install-xnj4qa\ClientForm-0.2.2\egg-dist-tmp-xfj60p
Adding clientform 0.2.2 to easy-install.pth file

Installed c:\tool2\python24\lib\site-packages\clientform-0.2.2-py2.4.egg

メッセージを眺めていると、どうもClientFormライブラリを自動的にダウンロードしてインストールしているようです。
なるほど、依存性を自動チェックして入れてくれるようですね。


早速、mechanizeを使ってみます。

F:\Wacky\Test\python>ipython

F:\Wacky\Test\python>python C:\tool2\Python24\Scripts\ipython
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 0.7.2 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import re

In [2]: from mechanize import Browser

In [3]: br = Browser()

In [4]: br.
br.__doc__                    br.add_proxy_password         br.request
br.__getattr__                br.addheaders                 br.request_class
br.__init__                   br.back                       br.response
br.__module__                 br.clear_history              br.retrieve
br.__str__                    br.click                      br.select_form
br._add_referer_header        br.click_link                 br.set_cookiejar
br._any_request               br.close                      br.set_debug_http
br._any_response              br.default_features           br.set_debug_redirects
br._call_chain                br.default_others             br.set_debug_responses
br._factory                   br.default_schemes            br.set_handle_equiv
br._filter_links              br.encoding                   br.set_handle_gzip
br._handle_referer            br.error                      br.set_handle_redirect
br._handler_index_valid       br.find_link                  br.set_handle_referer
br._history                   br.follow_link                br.set_handle_refresh
br._maybe_reindex_handlers    br.form                       br.set_handle_robots
br._mech_open                 br.forms                      br.set_handled_schemes
br._open                      br.geturl                     br.set_password_manager
br._password_manager          br.handle_error               br.set_proxies
br._proxy_password_manager    br.handle_open                br.set_proxy_password_manager
br._replace_handler           br.handler_classes            br.set_response
br._request                   br.handlers                   br.submit
br._response                  br.links                      br.title
br._set_handler               br.open                       br.viewing_html
br._ua_handlers               br.process_request            br.__class__
br.add_handler                br.process_response
br.add_password               br.reload

In [4]: br.open('http://www.google.com/')
Out[4]: <response_seek_wrapper at 0xf7d8f0 whose wrapped object = <closeable_response at 0xf7d8c8 whose fp = <socket._fileobject object at 0x00F79A40>>>

In [6]: br.title()
Out[6]: 'Google'

In [7]: br.viewing_html()
Out[7]: True

In [10]: for i in br.links():
   ....:     print i
   ....:
   ....:
Link(base_url='http://www.google.co.jp/', url='/url?sa=p&pref=ig&pval=3&q=http://www.google.co.jp/ig%3Fhl%3Dja', text='\x83p\x81[\x83\\\x83i\x83\x89\x83C\x83Y\x83h \x83z\x81[\x83\x80', tag='a', attrs=[('href', '/url?sa=p&pref=ig&pval=3&q=http://www.google.co.jp/ig%3Fhl%3Dja')])
...

結構簡単にアクセスできるもんですね。


と、ここまで来て、mechanizeのソースを眺めてみようと思い、'lib'フォルダ下のmechanize.pyを探してみましたが、どうも見つかりません。
強いて挙げるならば、lib/site-packages フォルダに、以下のファイルがあります。

C:\tool2\Python24\Lib\site-packages>dir *.egg
 ドライブ C のボリューム ラベルがありません。
 ボリューム シリアル番号は 4065-FD4A です

 C:\tool2\Python24\Lib\site-packages のディレクトリ

2006/08/06  16:31            68,591 clientform-0.2.2-py2.4.egg
2006/08/06  16:31           171,193 mechanize-0.1.2b-py2.4.egg
2006/08/06  16:19    <DIR>          setuptools-0.6c1-py2.4.egg

そういや、eggファイルって、中身ZIPファイルだったっけなぁ。と思い出し、一応チェックします。
Cygwinのunzipコマンドを使って、ファイルを列挙…。

$ unzip -l mechanize-0.1.2b-py2.4.egg
Archive:  mechanize-0.1.2b-py2.4.egg
  Length     Date   Time    Name
 --------    ----   ----    ----
        1  08-06-06 16:31   EGG-INFO/dependency_links.txt
     2659  08-06-06 16:31   EGG-INFO/PKG-INFO
       24  08-06-06 16:31   EGG-INFO/requires.txt
     1146  08-06-06 16:31   EGG-INFO/SOURCES.txt
       10  08-06-06 16:31   EGG-INFO/top_level.txt
        0  08-06-06 16:31   EGG-INFO/zip-safe
    16975  08-06-06 16:31   mechanize/_auth.py
    15855  08-06-06 16:31   mechanize/_auth.pyc
    63289  08-06-06 16:31   mechanize/_clientcookie.py
    54082  08-06-06 16:31   mechanize/_clientcookie.pyc
     3291  08-06-06 16:31   mechanize/_gzip.py
     4086  08-06-06 16:31   mechanize/_gzip.pyc
     8122  08-06-06 16:31   mechanize/_headersutil.py
     7939  08-06-06 16:31   mechanize/_headersutil.pyc
    18981  08-06-06 16:31   mechanize/_html.py
    18504  08-06-06 16:31   mechanize/_html.pyc
     7189  08-06-06 16:31   mechanize/_lwpcookiejar.py
     6580  08-06-06 16:31   mechanize/_lwpcookiejar.pyc
    19979  08-06-06 16:31   mechanize/_mechanize.py
    18499  08-06-06 16:31   mechanize/_mechanize.pyc
     6249  08-06-06 16:31   mechanize/_mozillacookiejar.py
     5261  08-06-06 16:31   mechanize/_mozillacookiejar.pyc
    14692  08-06-06 16:31   mechanize/_msiecookiejar.py
    12180  08-06-06 16:31   mechanize/_msiecookiejar.pyc
     8922  08-06-06 16:31   mechanize/_opener.py
     7239  08-06-06 16:31   mechanize/_opener.pyc
    12473  08-06-06 16:31   mechanize/_pullparser.py
    14495  08-06-06 16:31   mechanize/_pullparser.pyc
     2294  08-06-06 16:31   mechanize/_request.py
     2606  08-06-06 16:31   mechanize/_request.pyc
     1378  08-06-06 16:31   mechanize/_urllib2.py
     2590  08-06-06 16:31   mechanize/_urllib2.pyc
    24840  08-06-06 16:31   mechanize/_urllib2_support.py
    23690  08-06-06 16:31   mechanize/_urllib2_support.pyc
    12338  08-06-06 16:31   mechanize/_useragent.py
    10951  08-06-06 16:31   mechanize/_useragent.pyc
    20523  08-06-06 16:31   mechanize/_util.py
    20055  08-06-06 16:31   mechanize/_util.pyc
     1245  08-06-06 16:31   mechanize/__init__.py
     2083  08-06-06 16:31   mechanize/__init__.pyc
 --------                   -------
   473315                   40 files

はい、確かに、eggファイルは、中身ZIPファイルです。


と、ファイル一覧を眺めていると、lib/site-packagesに、easy-install.pthってファイルがあります。

$ cat easy-install.pth
import sys; sys.__plen = len(sys.path)
.\setuptools-0.6c1-py2.4.egg
.\mechanize-0.1.2b-py2.4.egg
.\clientform-0.2.2-py2.4.egg
import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)

なるほど、どうもeggファイルをライブラリにアタッチできるようにしているようですね。
ま〜、つまり、WindowsのZIPファイルを圧縮フォルダとして扱える機能みたいなもんでしょうか。


なんで、.egg拡張子を、適当なアーカイバに設定しておきました。
とりあえず、Archon2に設定してみたんですが、ちゃんと表示されます。ふむふむ。
ただ、ビューワを使うと拡張子がZIPでない悪影響が発生する模様。(テキストエディタD&Dできますので、とりあえず無問題)
う〜む、もっと別のアーカイバを探すしかないかなぁ。