LoginSignup
10
10

More than 5 years have passed since last update.

ScraperWikiを使ってWEBサイトからデータを定期的に取得する

Last updated at Posted at 2014-01-22

ScraperWiki」サービスを利用すれば、自分でサーバーなどを借りなくてもウェブスクレイピングを定期的に行うことができます。

ScraperWikiの特徴

  • スクレイパーのスクリプトをブラウザ上で編集・実行できる
  • スクリプトを定期的に実行できる
  • 取得したデータをcsvでエクスポートしたりJSON APIを通じて取得したりできる
  • 取得したデータをテーブルやグラフとして表示できる
  • 3データセットまで無料

スクリーンショット

スクリプト編集画面
scraperwiki201401221.JPG

DB
201401222.JPG

スクレイパースクリプト サンプル

#!/usr/bin/env python
import scraperwiki
import lxml.html
import json

url = "http://target.website.hoge/index.html" #スクレイピングするターゲットサイト
html = scraperwiki.scrape(url)      #htmlドキュメント取得
root = lxml.html.fromstring(html)   #rootエレメントオブジェクト取得

data = []
id = 0
for el in root.cssselect("#hoge_contents > li > span"):  #cssセレクタで要素を抽出
    data.append({'id':id, 'text':el.text }) #抽出した要素のテキストを保存
    id = id + 1

print repr(data)    #保存したデータをコンソールに出力


# Saving data:
unique_keys = [ 'id' ] #ユニークキーを指定
scraperwiki.sql.save(unique_keys, data) #DBに保存

実際に使用した例
http://shimz.me/blog/d3-js/3353

10
10
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10