Web Scraping

Python で書かれた Web Scraping フレームワークのをちらっと見てみた。

pypi.python.org

 特徴は、

Automatic cookies (session) support
HTTP and SOCKS proxy with and without authorization
Keep-Alive support
IDN support
Tools to work with web forms
Easy multipart file uploading
Flexible customization of HTTP requests
Automatic charset detection
Powerful API of extracting info from HTML documents with XPATH queries
Asynchronous API to make thousands of simultaneous queries. This part of library called Spider and it is too big to even list its features in this README.
Python 3 ready

ということで、なんか便利そうな感じがするので見てみる。依存関係があるのは、

  • lxml
  • pycurl
  • selection
  • weblib
  • six

インストールは、

pip istall -U lxml pycurl selection weblib six
pip install -U Grab

サイトにあるサンプルはこんな感じ。

from grab import Grab

g = Grab()
g.go('https://github.com/login')
g.set_input('login', 'lorien')
g.set_input('password', '***')
g.submit()
for elem in g.doc.select('//ul[@id="repo_listing"]/li/a'):
print('%s: %s' % (elem.text(), elem.attr('href')))

ログインしてから、ちょっとなにか取ってくるというのが簡単に書ける。

忙しいのが一段落したら、この手のものは、あれこれあると思うので、比較しながらちょっと使ってみようかな。