RubyScraping

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites

    RubyScraping - Presentation Transcript

    1. RubyScraping Ruby関西 yhara(原 悠)
    2. 自己紹介 yhara (原 悠) 滋賀県在住 所属:京大マイコンクラブ 過去の発表: Plagger meets Ruby 30分でわかるcallccの使い方 Ruby用Webスクレイピングライブラリ、Hpricot
    3. “RubyScraping” Wiki http://mono.kmc.gr.jp/~yhara/rubyscraping/
    4. 「スクレイピング」とは? scrape=引っかく、削り取る Webページから情報を抜き出す技術 便利なライブラリ WWW::Mechanize Hpricot
    5. WWW::Mechanize Web自動巡回ライブラリ
    6. WWW::Mechanize Web自動巡回ライブラリ 例:自動google検索 require 'mechanize' agent = WWW::Mechanize.new page = agent.get(\"http://www.google.co.jp/\") search_form = page.forms.first search_form[\"q\"] = \"Ruby\" search_results = search_form.submit print search_results.body
    7. 実行結果 googleで”Ruby”を検索した結果のHTMLが 出力される
    8. 解説 手作業で検索するときの動作: (1)トップページを開く (2)検索用フォームを探す (3)検索語句に ”Ruby” と入力 (4)「検索」ボタンを押す Mechanizeならこれを「そのまま」Rubyで書ける!
    9. 解説 (1)トップページを開く→(2)検索用フォームを探す→ (3)検索語句に ”Ruby” と入力→(4)「検索」ボタンを押す require 'mechanize' agent = WWW::Mechanize.new page = agent.get(“http://www.google.co.jp/”) #1 search_form = page.forms.first #2 search_form[“q”] = “Ruby“ #3 search_results = search_form.submit #4 print search_results.body
    10. Hpricot HTML解析ライブラリ
    11. Hpricot HTML解析ライブラリ 例:さっきのgoogle検索結果から、 ヒットしたページのタイトルを全て出力 require \"hpricot\" doc = Hpricot(File.read(\"google.html\")) (doc/\"a.l\").each do |a| puts a[\"href\"] puts a.inner_text end
    12. 実行結果
    13. 解説 (doc/\"a.l\") ⇔ <a class=\"l\">…</a> (doc/\"div#res\") ⇔ <div id=\"res\">…</div> CSSセレクタが使える require \"hpricot\" doc = Hpricot(File.read(\"google.html\")) (doc/\"a.l\").each do |a| puts a[\"href\"] #=> URL puts a.inner_text #=> ページタイトル end
    14. デモ (時間が余れば)
    15. Googleの広告を消す(^^; require ’rubygems’ require ’hpricot’ require ‘kconv’ html = File.read(“google.html”) #htmlファイルを開く doc = Hpricot(html) #Hpricotオブジェクトを作る #テキストに”DODA”が含まれているtdタグの中身を消す (doc/”td[text()*=DODA]”).empty File.open(“no_ad.html”, “w”){|f| #ファイルに書き戻す f.wirte doc.to_html }
    16. 実行結果
    17. その他の機能 ファイルのアップロード (Mechanize) プロキシ使用(Mechanize) CSSセレクタ (Hpricot) XPath (Hpricot) タグ構造を調べるには、Firefox+FireBugが 便利
    18. 続きはWebで! ご清聴ありがとうございました
    19. おまけ cross-search.rb > ./cross_search.rb Ruby search_by_google: オブジェクト指向スクリプト言語 Ruby search_by_excite: オブジェクト指向スクリプト言語 Ruby search_by_yahoo: オブジェクト指向スクリプト言語 Ruby search_by_msn: Rubyリファレンスマニュアル - Enumerable なぜかMSNサーチだけリファレンスのEnumerableがトップに来る(^^;

    + yharayhara, 3 years ago

    custom

    2364 views, 2 favs, 0 embeds more stats

    A short presentation about Ruby's scraping library, more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 2364
      • 2364 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 116
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories