Your SlideShare is downloading. ×
0
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Web::Scraper
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Web::Scraper

27,150

Published on

Published in: Technology
3 Comments
25 Likes
Statistics
Notes
No Downloads
Views
Total Views
27,150
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
558
Comments
3
Likes
25
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Practical Web Scraping with Web::Scraper Tatsuhiko Miyagawa [email_address] Six Apart, Ltd. / Shibuya Perl Mongers YAPC::Europe 2007 Vienna
  • 2. <ul><li>Tatsuhiko Miyagawa </li></ul>
  • 3. <ul><li>CPAN: MIYAGAWA </li></ul>
  • 4. abbreviation Acme::Module::Authors Acme::Sneeze Acme::Sneeze::JP Apache::ACEProxy Apache::AntiSpam Apache::Clickable Apache::CustomKeywords Apache::DefaultCharset Apache::GuessCharset Apache::JavaScript::DocumentWrite Apache::No404Proxy Apache::Profiler Apache::Session::CacheAny Apache::Session::Generate::ModUniqueId Apache::Session::Generate::ModUsertrack Apache::Session::PHP Apache::Session::Serialize::YAML Apache::Singleton Apache::StickyQuery Archive::Any::Create Attribute::Profiled Attribute::Protected Attribute::Unimplemented Bundle::Sledge capitalization Catalyst::Plugin::JSONRPC Catalyst::View::Jemplate Catalyst::View::JSON CGI::Untaint::email Class::DBI::AbstractSearch Class::DBI::Extension Class::DBI::Pager Class::DBI::Replication Class::DBI::SQLite Class::DBI::View Class::Trigger Convert::Base32 Convert::DUDE Convert::RACE Date::Japanese::Era Date::Range::Birth Device::KeyStroke::Mobile Dunce::time Email::Find Email::Valid::Loose Encode::JavaScript::UCS Encode::JP::Mobile Encode::Punycode File::Find::Rule::Digest Geo::Coder::Google HTML::Entities::ImodePictogram HTML::RelExtor HTML::ResolveLink HTML::XSSLint HTTP::MobileAgent HTTP::ProxyPAC HTTP::Server::Simple::Authen IDNA::Punycode Inline::Basic Inline::TT JSON::Syck Kwiki::Emoticon Kwiki::Export Kwiki::Footnote Kwiki::OpenSearch Kwiki::OpenSearch::Service Kwiki::TypeKey Kwiki::URLBL Log::Dispatch::Config Log::Dispatch::DBI Mac::Macbinary Mail::Address::MobileJp Mail::ListDetector::Detector::Fml MSIE::MenuExt Net::DAAP::Server::AAC Net::IDN::Nameprep Net::IPAddr::Find Net::YahooMessenger NetAddr::IP::Find PHP::Session plagger Plagger POE::Component::Client::AirTunes POE::Component::YahooMessenger Template::Plugin::Clickable Template::Plugin::Comma Template::Plugin::FillInForm Template::Plugin::HTML::Template Template::Plugin::JavaScript Template::Plugin::MobileAgent Template::Plugin::Shuffle Template::Provider::Encoding Term::Encoding Term::TtyRec Text::Emoticon Text::Emoticon::GoogleTalk Text::Emoticon::MSN Text::Emoticon::Yahoo Text::MessageFormat Time::Duration::ja Time::Duration::Parse Web::Scrape WebService::Bloglines WebService::ChangesXml WebService::Google::Suggest WWW::Baseball::NPB WWW::Blog::Metadata::MobileLinkDiscovery WWW::Blog::Metadata::OpenID WWW::Blog::Metadata::OpenSearch WWW::Cache::Google WWW::OpenSearch XML::Atom XML::Atom::Lifeblog XML::Atom::Stream XML::Liberal
  • 5.  
  • 6. http://code.sixapart.com/
  • 7.  
  • 8. <ul><li>Practical Web Scraping </li></ul><ul><li>with Web::Scraper </li></ul>
  • 9. 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
  • 10. 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
  • 11. <ul><li>&quot;Screen-scraping </li></ul><ul><li>is so 1999!&quot; </li></ul>
  • 12.  
  • 13.  
  • 14. <ul><li>RSS is a metadata </li></ul><ul><li>not a complete </li></ul><ul><li>HTML replacement </li></ul>
  • 15. <ul><li>Practical Web Scraping </li></ul><ul><li>with Web::Scraper </li></ul>
  • 16. <ul><li>What's wrong with </li></ul><ul><li>LWP & Regexp? </li></ul>
  • 17.  
  • 18. <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 />
  • 19. <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. <ul><li>It works! </li></ul>
  • 21. WWW::MySpace 0.70
  • 22. WWW::Search::Ebay 2.231
  • 23. WWW::Mixi 0.50
  • 24. <ul><li>It works … </li></ul>
  • 25. <ul><li>There are </li></ul><ul><li>3 problems </li></ul><ul><li>(at least) </li></ul>
  • 26. <ul><li>(1) </li></ul><ul><li>Fragile </li></ul><ul><li>Easy to break even with slight HTML changes </li></ul><ul><li>(like newlines, order of attributes etc.) </li></ul>
  • 27. <ul><li>(2) </li></ul><ul><li>Hard to maintain </li></ul><ul><li>Regular expression based scrapers are good </li></ul><ul><li>Only when they're used in write-only scripts </li></ul>
  • 28. <ul><li>(3) </li></ul><ul><li>Improper </li></ul><ul><li>HTML & encoding </li></ul><ul><li>handling </li></ul>
  • 29. <span class=&quot;message&quot;>I &hearts; Vienna</span> > perl –e '$c =~ m@<span class=&quot;message&quot;>(.*?)</span>@ and print $1' I &hearts; Vienna
  • 30. <span class=&quot;message&quot;>I &hearts; Vienna</span> > perl –MHTML::Entities –e '$c =~ m@<span class=&quot;message&quot;>(.*?)</span>@ and print decode_entities ($1)' I ♥ Vienna
  • 31. <span class=&quot;message&quot;> ウィーンが大好き! </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. ウィーンが大好き!
  • 32. <ul><li>The &quot;right&quot; way </li></ul><ul><li>of screen-scraping </li></ul>
  • 33. <ul><li>(1), (2) </li></ul><ul><li>Maintainable </li></ul><ul><li>Less fragile </li></ul>
  • 34. <ul><li>Use XPath </li></ul><ul><li>and CSS Selectors </li></ul>
  • 35. <ul><li>XPath </li></ul><ul><li>HTML::TreeBuilder::XPath </li></ul><ul><li>XML::LibXML </li></ul>
  • 36. 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
  • 37. <ul><li>CSS Selectors </li></ul><ul><li>&quot;XPath for HTML coders&quot; </li></ul><ul><li>&quot;XPath for people who hates XML&quot; </li></ul>
  • 38. CSS Selectors <ul><li>body { font-size: 12px; } </li></ul><ul><li>div.article { padding: 1em } </li></ul><ul><li>span#count { color: #fff } </li></ul>
  • 39. <ul><li>XPath: </li></ul><ul><li>//strong[@id=&quot;ctu&quot;] </li></ul><ul><li>CSS Selector: </li></ul><ul><li>strong#ctu </li></ul>
  • 40. 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
  • 41. 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;
  • 42. <ul><li>Robust, </li></ul><ul><li>Maintainable, </li></ul><ul><li>and </li></ul><ul><li>Sane character handling </li></ul>
  • 43. 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
  • 44. 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;
  • 45. <ul><li>but … </li></ul><ul><li>long and boring </li></ul>
  • 46. <ul><li>Practical Web Scraping </li></ul><ul><li>with Web::Scraper </li></ul>
  • 47. <ul><li>Web scraping toolkit </li></ul><ul><li>inspired by scrapi.rb </li></ul><ul><li>DSL-ish </li></ul>
  • 48. 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;
  • 49. Example (after) <ul><li>#!/usr/bin/perl </li></ul><ul><li>use strict; </li></ul><ul><li>use warnings; </li></ul><ul><li>use Web::Scraper; </li></ul><ul><li>use URI; </li></ul><ul><li>my $s = scraper { </li></ul><ul><li>process &quot;strong#ctu&quot;, time => 'TEXT'; </li></ul><ul><li>result 'time'; </li></ul><ul><li>}; </li></ul><ul><li>my $uri = URI->new(&quot;http://timeanddate.com/worldclock/&quot;); </li></ul><ul><li>print $s->scrape($uri); </li></ul>
  • 50. Basics <ul><li>use Web::Scraper; </li></ul><ul><li>my $s = scraper { </li></ul><ul><li># DSL goes here </li></ul><ul><li>}; </li></ul><ul><li>my $res = $s->scrape($uri); </li></ul>
  • 51. process <ul><li>process $selector, </li></ul><ul><li>$key => $what, </li></ul><ul><li>… ; </li></ul>
  • 52. <ul><li>$selector: </li></ul><ul><li>CSS Selector </li></ul><ul><li>or </li></ul><ul><li>XPath (start with /) </li></ul>
  • 53. <ul><li>$key: </li></ul><ul><li>key for the result hash </li></ul><ul><li>append &quot;[]&quot; for looping </li></ul>
  • 54. <ul><li>$what: </li></ul><ul><li>'@attr' </li></ul><ul><li>'TEXT' </li></ul><ul><li>Web::Scraper </li></ul><ul><li>sub { … } </li></ul><ul><li>Hash reference </li></ul>
  • 55. <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>
  • 56. <ul><li>process &quot;ul.sites > li > a&quot;, </li></ul><ul><li>'urls[]' => ' @href '; </li></ul><ul><li># { urls => [ … ] } </li></ul><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>
  • 57. <ul><li>process '//ul[@class=&quot;sites&quot;]/li/a', </li></ul><ul><li>'names[]' => ' TEXT '; </li></ul><ul><li># { names => [ 'OpenGuides', … ] } </li></ul><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>
  • 58. <ul><li>process &quot;ul.sites > li&quot;, </li></ul><ul><li>'sites[]' => scraper { </li></ul><ul><li>process 'a', </li></ul><ul><li>link => '@href', name => 'TEXT'; </li></ul><ul><li>}; </li></ul><ul><li># { sites => [ { link => …, name => … }, </li></ul><ul><li># { link => …, name => … } ] }; </li></ul><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>
  • 59. <ul><li>process &quot;ul.sites > li > a&quot;, </li></ul><ul><li>'sites[]' => sub { </li></ul><ul><li># $_ is HTML::Element </li></ul><ul><li>+{ link => $_->attr('href'), name => $_->as_text }; </li></ul><ul><li>}; </li></ul><ul><li># { sites => [ { link => …, name => … }, </li></ul><ul><li># { link => …, name => … } ] }; </li></ul><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>
  • 60. <ul><li>process &quot;ul.sites > li > a&quot;, </li></ul><ul><li>'sites[]' => { </li></ul><ul><li>link => '@href', name => 'TEXT'; </li></ul><ul><li>}; </li></ul><ul><li># { sites => [ { link => …, name => … }, </li></ul><ul><li># { link => …, name => … } ] }; </li></ul><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>
  • 61. result <ul><li>result; </li></ul><ul><li># get stash as hashref (default) </li></ul><ul><li>result @keys; </li></ul><ul><li># get stash as hashref containing @keys </li></ul><ul><li>result $key; </li></ul><ul><li># get value of stash $key; </li></ul>my $s = scraper { process …; process …; result 'foo', 'bar'; };
  • 62. <ul><li>More Examples </li></ul>
  • 63.  
  • 64. Thumbnail URLs on Flickr set <ul><li>#!/usr/bin/perl </li></ul><ul><li>use strict; </li></ul><ul><li>use Data::Dumper; </li></ul><ul><li>use Web::Scraper; </li></ul><ul><li>use URI; </li></ul><ul><li>my $url = &quot;http://flickr.com/photos/bulknews/sets/72157601700510359/&quot;; </li></ul><ul><li>my $s = scraper { </li></ul><ul><li>process &quot;a.image_link img&quot;, &quot;thumbs[]&quot; => '@src'; </li></ul><ul><li>}; </li></ul><ul><li>warn Dumper $s->scrape( URI->new($url) ); </li></ul>
  • 65.  
  • 66. <span class=&quot;vcard&quot;> <a href=&quot;http://twitter.com/iamcal&quot; class=&quot;url&quot; rel=&quot;contact&quot; title=&quot;Cal Henderson&quot;> <img alt=&quot;Cal Henderson&quot; class=&quot;photo fn&quot; height=&quot;24&quot; id=&quot;profile-image&quot; src=&quot;http://assets0.twitter.com/…/mini/buddyicon.gif&quot; width=&quot;24&quot; /></a> </span> <span class=&quot;vcard&quot;> … </span>
  • 67. Twitter Friends <ul><li>#!/usr/bin/perl </li></ul><ul><li>use strict; </li></ul><ul><li>use Web::Scraper; </li></ul><ul><li>use URI; </li></ul><ul><li>use Data::Dumper; </li></ul><ul><li>my $url = &quot;http://twitter.com/miyagawa&quot;; </li></ul><ul><li>my $s = scraper { </li></ul><ul><li>process &quot;span.vcard a&quot;, &quot;people[]&quot; => '@title'; </li></ul><ul><li>}; </li></ul><ul><li>warn Dumper $s->scrape( URI->new($url) ) ; </li></ul>
  • 68. Twitter Friends (complex) <ul><li>#!/usr/bin/perl </li></ul><ul><li>use strict; </li></ul><ul><li>use Web::Scraper; </li></ul><ul><li>use URI; </li></ul><ul><li>use Data::Dumper; </li></ul><ul><li>my $url = &quot;http://twitter.com/miyagawa&quot;; </li></ul><ul><li>my $s = scraper { </li></ul><ul><li>process &quot;span.vcard&quot;, &quot;people[]&quot; => scraper { </li></ul><ul><li>process &quot;a&quot;, link => '@href', name => '@title'; </li></ul><ul><li>process &quot;img&quot;, thumb => '@src'; </li></ul><ul><li>}; </li></ul><ul><li>}; </li></ul><ul><li>warn Dumper $s->scrape( URI->new($url) ) ; </li></ul>
  • 69. <ul><li>Tools </li></ul>
  • 70. <ul><li>> cpan Web::Scraper </li></ul><ul><li>comes with 'scraper' CLI </li></ul>
  • 71. <ul><li>> scraper http://example.com/ </li></ul><ul><li>scraper> process &quot;a&quot;, &quot;links[]&quot; => '@href'; </li></ul><ul><li>scraper> d </li></ul><ul><li>$VAR1 = { </li></ul><ul><li>links => [ </li></ul><ul><li>'http://example.org/', </li></ul><ul><li>'http://example.net/', </li></ul><ul><li>], </li></ul><ul><li>}; </li></ul><ul><li>scraper> y </li></ul><ul><li>--- </li></ul><ul><li>links: </li></ul><ul><li>- http://example.org/ </li></ul><ul><li>- http://example.net/ </li></ul>
  • 72. <ul><li>> scraper /path/to/foo.html </li></ul><ul><li>> GET http://example.com/ | scraper </li></ul>
  • 73. <ul><li>TODO </li></ul>
  • 74. <ul><li>Web::Scraper </li></ul><ul><li>Needs documentation </li></ul>
  • 75. <ul><li>More examples </li></ul><ul><li>to put in eg/ directory </li></ul>
  • 76. <ul><li>integrate with </li></ul><ul><li>WWW::Mechanize </li></ul><ul><li>and Test::WWW::Declare </li></ul>
  • 77. <ul><li>XPath Auto-suggestion </li></ul><ul><li>off of DOM + element </li></ul><ul><li>DOM + XPath => Element </li></ul><ul><li>DOM + Element => XPath? </li></ul><ul><li>(Template::Extract?) </li></ul>
  • 78. <ul><li>Questions? </li></ul>
  • 79. <ul><li>Thank you </li></ul><ul><li>http://search.cpan.org/dist/Web-Scraper </li></ul><ul><li>http://www.slideshare.net/miyagawa/webscraper </li></ul>

×