Web Scraper Shibuya.pm tech talk #8

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.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

Post a comment
Embed Video
Edit your comment Cancel

5 Favorites & 3 Groups

Web Scraper Shibuya.pm tech talk #8 - Presentation Transcript

  1. Practical Web Scraping with Web::Scraper Tatsuhiko Miyagawa [email_address] Six Apart, Ltd. / Shibuya Perl Mongers Shibuya.pm Tech Talks #8
    • Practical Web Scraping
    • with Web::Scraper
  2. Web pages are built using text-based mark-up languages ( HTML and XHTML ), and frequently contain a wealth of useful data in text form. However, most web pages are designed for human consumption, and frequently mix content with presentation. Thus, screen scrapers were reborn in the web era to extract machine-friendly data from HTML and other markup. http://en.wikipedia.org/wiki/Screen_scraping
  3. Web pages are built using text-based mark-up languages ( HTML and XHTML ), and frequently contain a wealth of useful data in text form. However, most web pages are designed for human consumption, and frequently mix content with presentation. Thus, screen scrapers were reborn in the web era to extract machine-friendly data from HTML and other markup. http://en.wikipedia.org/wiki/Screen_scraping
    • "Screen-scraping
    • is so 1999!"
  4.  
  5.  
    • RSS is a metadata
    • not a complete
    • HTML replacement
    • Practical Web Scraping
    • with Web::Scraper
    • What's wrong with
    • LWP & Regexp?
  6.  
  7. <td>Current <strong>UTC</strong> (or GMT/Zulu)-time used: <strong id=&quot;ctu&quot;>Monday, August 27, 2007 at 12:49:46</strong> <br />
  8. <td>Current <strong>UTC</strong> (or GMT/Zulu)-time used: <strong id=&quot;ctu&quot;>Monday, August 27, 2007 at 12:49:46</strong> <br /> > perl -MLWP::Simple -le '$c = get(&quot;http://timeanddate.com/worldclock/&quot;); $c =~ m@<strong id=&quot;ctu&quot;>(.*?)</strong>@ and print $1' Monday, August 27, 2007 at 12:49:46
    • It works!
  9. WWW::MySpace 0.70
  10. WWW::Search::Ebay 2.231
  11. WWW::Mixi 0.50
    • It works …
    • There are
    • 3 problems
    • (at least)
    • (1)
    • Fragile
    • Easy to break even with slight HTML changes
    • (like newlines, order of attributes etc.)
    • (2)
    • Hard to maintain
    • Regular expression based scrapers are good
    • Only when they're used in write-only scripts
    • (3)
    • Improper
    • HTML & encoding
    • handling
  12. <span class=&quot;message&quot;>I &hearts; Shibuya</span> > perl –e '$c =~ m@<span class=&quot;message&quot;>(.*?)</span>@ and print $1' I &hearts; Shibuya
  13. <span class=&quot;message&quot;>I &hearts; Shibuya</span> > perl –MHTML::Entities –e '$c =~ m@<span class=&quot;message&quot;>(.*?)</span>@ and print decode_entities ($1)' I ♥ Shibuya
  14. <span class=&quot;message&quot;>Perl が大好き! </span> > perl –MHTML::Entities –MEncode –e '$c =~ m@<span class=&quot;message&quot;>(.*?)</span>@ and print decode_entities( decode_utf8 ($1))' Wide character in print at –e line 1. Perl が大好き!
    • The &quot;right&quot; way
    • of screen-scraping
    • (1), (2)
    • Maintainable
    • Less fragile
    • Use XPath
    • and CSS Selectors
    • XPath
    • HTML::TreeBuilder::XPath
    • XML::LibXML
  15. XPath <td>Current <strong>UTC</strong> (or GMT/Zulu)-time used: <strong id=&quot;ctu&quot;>Monday, August 27, 2007 at 12:49:46</strong> <br /> use HTML::TreeBuilder::XPath; my $tree = HTML::TreeBuilder::XPath->new_from_content($content); print $tree->findnodes ('//strong[@id=&quot;ctu&quot;]') ->shift->as_text; # Monday, August 27, 2007 at 12:49:46
    • CSS Selectors
    • &quot;XPath for HTML coders&quot;
    • &quot;XPath for people who hates XML&quot;
  16. CSS Selectors
    • body { font-size: 12px; }
    • div.article { padding: 1em }
    • span#count { color: #fff }
    • XPath:
    • //strong[@id=&quot;ctu&quot;]
    • CSS Selector:
    • strong#ctu
  17. CSS Selectors <td>Current <strong>UTC</strong> (or GMT/Zulu)-time used: <strong id=&quot;ctu&quot;>Monday, August 27, 2007 at 12:49:46</strong> <br /> use HTML::TreeBuilder::XPath; use HTML::Selector::XPath qw(selector_to_xpath); my $tree = HTML::TreeBuilder::XPath->new_from_content($content); my $xpath = selector_to_xpath &quot;strong#ctu&quot;; print $tree->findnodes($xpath)->shift->as_text; # Monday, August 27, 2007 at 12:49:46
  18. Complete Script #!/usr/bin/perl use strict; use warnings; use Encode; use LWP::UserAgent; use HTTP::Response::Encoding; use HTML::TreeBuilder::XPath; use HTML::Selector::XPath qw(selector_to_xpath); my $ua = LWP::UserAgent->new; my $res = $ua->get(&quot;http://www.timeanddate.com/worldclock/&quot;); if ($res->is_error) { die &quot;HTTP GET error: &quot;, $res->status_line; } my $content = decode $res->encoding, $res->content; my $tree = HTML::TreeBuilder::XPath->new_from_content($content); my $xpath = selector_to_xpath(&quot;strong#ctu&quot;); my $node = $tree->findnodes($xpath)->shift; print $node->as_text;
    • Robust,
    • Maintainable,
    • and
    • Sane character handling
  19. Exmaple (before) <td>Current <strong>UTC</strong> (or GMT/Zulu)-time used: <strong id=&quot;ctu&quot;>Monday, August 27, 2007 at 12:49:46</strong> <br /> > perl -MLWP::Simple -le '$c = get(&quot;http://timeanddate.com/worldclock/&quot;); $c =~ m@<strong id=&quot;ctu&quot;>(.*?)</strong>@ and print $1' Monday, August 27, 2007 at 12:49:46
  20. Example (after) #!/usr/bin/perl use strict; use warnings; use Encode; use LWP::UserAgent; use HTTP::Response::Encoding; use HTML::TreeBuilder::XPath; use HTML::Selector::XPath qw(selector_to_xpath); my $ua = LWP::UserAgent->new; my $res = $ua->get(&quot;http://www.timeanddate.com/worldclock/&quot;); if ($res->is_error) { die &quot;HTTP GET error: &quot;, $res->status_line; } my $content = decode $res->encoding, $res->content; my $tree = HTML::TreeBuilder::XPath->new_from_content($content); my $xpath = selector_to_xpath(&quot;strong#ctu&quot;); my $node = $tree->findnodes($xpath)->shift; print $node->as_text;
    • but …
    • long and boring
    • Practical Web Scraping
    • with Web::Scraper
    • Web scraping toolkit
    • inspired by scrapi.rb
    • DSL-ish
  21. Example (before) #!/usr/bin/perl use strict; use warnings; use Encode; use LWP::UserAgent; use HTTP::Response::Encoding; use HTML::TreeBuilder::XPath; use HTML::Selector::XPath qw(selector_to_xpath); my $ua = LWP::UserAgent->new; my $res = $ua->get(&quot;http://www.timeanddate.com/worldclock/&quot;); if ($res->is_error) { die &quot;HTTP GET error: &quot;, $res->status_line; } my $content = decode $res->encoding, $res->content; my $tree = HTML::TreeBuilder::XPath->new_from_content($content); my $xpath = selector_to_xpath(&quot;strong#ctu&quot;); my $node = $tree->findnodes($xpath)->shift; print $node->as_text;
  22. Example (after)
    • #!/usr/bin/perl
    • use strict;
    • use warnings;
    • use Web::Scraper;
    • use URI;
    • my $s = scraper {
    • process &quot;strong#ctu&quot;, time => 'TEXT';
    • result 'time';
    • };
    • my $uri = URI->new(&quot;http://timeanddate.com/worldclock/&quot;);
    • print $s->scrape($uri);
  23. Basics
    • use Web::Scraper;
    • my $s = scraper {
    • # DSL goes here
    • };
    • my $res = $s->scrape($uri);
  24. process
    • process $selector,
    • $key => $what,
    • … ;
    • $selector:
    • CSS Selector
    • or
    • XPath (start with /)
    • $key:
    • key for the result hash
    • append &quot;[]&quot; for looping
    • $what:
    • '@attr'
    • 'TEXT'
    • 'RAW'
    • Web::Scraper
    • sub { … }
    • Hash reference
  25. <ul class=&quot;sites&quot;> <li><a href=&quot;http://vienna.openguides.org/&quot;>OpenGuides</a></li> <li><a href=&quot;http://vienna.yapceurope.org/&quot;>YAPC::Europe</a></li> </ul>
    • process &quot;ul.sites > li > a&quot;,
    • 'urls[]' => ' @href ';
    • # { urls => [ … ] }
    <ul class=&quot;sites&quot;> <li><a href=&quot; http://vienna.openguides.org/ &quot;>OpenGuides</a></li> <li><a href=&quot; http://vienna.yapceurope.org/ &quot;>YAPC::Europe</a></li> </ul>
    • process '//ul[@class=&quot;sites&quot;]/li/a',
    • 'names[]' => ' TEXT ';
    • # { names => [ 'OpenGuides', … ] }
    <ul class=&quot;sites&quot;> <li><a href=&quot;http://vienna.openguides.org/&quot;> OpenGuides </a></li> <li><a href=&quot;http://vienna.yapceurope.org/&quot;> YAPC::Europe </a></li> </ul>
    • process &quot;ul.sites > li&quot;,
    • 'sites[]' => scraper {
    • process 'a',
    • link => '@href', name => 'TEXT';
    • };
    • # { sites => [ { link => …, name => … },
    • # { link => …, name => … } ] };
    <ul class=&quot;sites&quot;> <li><a href=&quot;http://vienna.openguides.org/&quot;>OpenGuides</a></li> <li><a href=&quot;http://vienna.yapceurope.org/&quot;>YAPC::Europe</a></li> </ul>
    • process &quot;ul.sites > li > a&quot;,
    • 'sites[]' => sub {
    • # $_ is HTML::Element
    • +{ link => $_->attr('href'), name => $_->as_text };
    • };
    • # { sites => [ { link => …, name => … },
    • # { link => …, name => … } ] };
    <ul class=&quot;sites&quot;> <li><a href=&quot;http://vienna.openguides.org/&quot;>OpenGuides</a></li> <li><a href=&quot;http://vienna.yapceurope.org/&quot;>YAPC::Europe</a></li> </ul>
    • process &quot;ul.sites > li > a&quot;,
    • 'sites[]' => {
    • link => '@href', name => 'TEXT';
    • };
    • # { sites => [ { link => …, name => … },
    • # { link => …, name => … } ] };
    <ul class=&quot;sites&quot;> <li><a href=&quot;http://vienna.openguides.org/&quot;>OpenGuides</a></li> <li><a href=&quot;http://vienna.yapceurope.org/&quot;>YAPC::Europe</a></li> </ul>
  26. result
    • result;
    • # get stash as hashref (default)
    • result @keys;
    • # get stash as hashref containing @keys
    • result $key;
    • # get value of stash $key;
    my $s = scraper { process …; process …; result 'foo', 'bar'; };
    • Live Demo
    • Tools
    • > cpan Web::Scraper
    • comes with 'scraper' CLI
    • > scraper http://example.com/
    • scraper> process &quot;a&quot;, &quot;links[]&quot; => '@href';
    • scraper> d
    • $VAR1 = {
    • links => [
    • 'http://example.org/',
    • 'http://example.net/',
    • ],
    • };
    • scraper> y
    • ---
    • links:
    • - http://example.org/
    • - http://example.net/
    • > scraper /path/to/foo.html
    • > GET http://example.com/ | scraper
    • Recent Updates
    • 0.13
    • 'c' and 'c all'
    • WARN in scraper
    • 0.14
    • automatic absolute URI for link elements (a@href, img@src)
    • 0.14 (cont.)
    • 'RAW' and 'HTML'
    • 0.15
    • $Web::Scraper::UserAgent
    • $scraper->user_agent
    • 0.19
    • support encoding detection w/ META tags
    • TODO
    • Web::Scraper
    • Needs documentation
    • More examples
    • to put in eg/ directory
    • Alternative API
    • inspired by scRUBYt!
    • OO Backend API
    • if you don't like the DSL
    • integrate with
    • WWW::Mechanize
    • and Test::WWW::Declare
    • XPath Auto-suggestion
    • off of DOM + element
    • DOM + XPath => Element
    • DOM + Element => XPath?
    • (Template::Extract?)
    • generic XML support
    • (e.g. RSS/Atom feeds)
    • extensible text filter
    • date, geo, hCards (microformats)
    <span class=&quot;entry-date&quot;>October 1st, 2007 17:13:31 +0900</span> process &quot;.entry-date&quot;, date => 'TEXT :rfc822 ';
    • Summary
    • Web::Scraper
    • inspired by scrapi
    • easy, fun, maintainable
    • & less fragile
    • CSS selector
    • XPath
    • Questions?
    • Thank you
    • http://search.cpan.org/dist/Web-Scraper
    • http://www.slideshare.net/miyagawa/webscraper

+ Tatsuhiko MiyagawaTatsuhiko Miyagawa, 3 years ago

custom

10691 views, 5 favs, 14 embeds more stats

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 10691
    • 9335 on SlideShare
    • 1356 from embeds
  • Comments 1
  • Favorites 5
  • Downloads 174
Most viewed embeds
  • 1075 views on http://blog.bulknews.net
  • 200 views on http://jeen.tistory.com
  • 51 views on http://www.techpresentations.org
  • 8 views on http://shibuyahacks.tumblr.com
  • 7 views on http://jeen.perlog.org

more

All embeds
  • 1075 views on http://blog.bulknews.net
  • 200 views on http://jeen.tistory.com
  • 51 views on http://www.techpresentations.org
  • 8 views on http://shibuyahacks.tumblr.com
  • 7 views on http://jeen.perlog.org
  • 4 views on http://d.hatena.ne.jp
  • 3 views on http://feeds.feedburner.com
  • 2 views on http://www5.atwiki.jp
  • 1 views on http://yapceu.plagger.org
  • 1 views on http://72.14.235.104
  • 1 views on http://feedpath.jp
  • 1 views on http://211.14.18.33
  • 1 views on http://209.85.175.104
  • 1 views on http://static.slideshare.net

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