<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sixserv blog &#187; mechanize</title>
	<atom:link href="http://sixserv.org/tag/mechanize/feed/" rel="self" type="application/rss+xml" />
	<link>http://sixserv.org</link>
	<description>A Blog about Linux, Networking, Development and Security.</description>
	<lastBuildDate>Tue, 27 Jul 2010 16:45:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Web scraping mit Ruby/Mechanize</title>
		<link>http://sixserv.org/2009/05/27/webscripting-mit-ruby-und-mechanize/</link>
		<comments>http://sixserv.org/2009/05/27/webscripting-mit-ruby-und-mechanize/#comments</comments>
		<pubDate>Wed, 27 May 2009 12:44:13 +0000</pubDate>
		<dc:creator>apoc</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[mechanize]]></category>
		<category><![CDATA[Networking]]></category>

		<guid isPermaLink="false">http://sixserv.org/?p=398</guid>
		<description><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ruby.png" width="50" height="51" alt="" title="Ruby" /><br/>Praktisch jede Interaktion mit einer Website oder Webapplikation kann gescriptet, d.h. automatisiert werden. Das Abgrasen von Webseiten nach bestimmten Informationen wird auch als Scraping bezeichnet(für die nicht menschlichen Besucher dieser Seite sei das erwähnt *g*) Scripte können einem eine ganze Menge Arbeit abnehmen und sogar Dinge tun, die manuell unmöglich wären. Ich beschäftige mich mit [...]]]></description>
			<content:encoded><![CDATA[<img src="/wp-content/themes/6stheme/icons/icon_ruby.png" width="50" height="51" alt="" title="Ruby" /><br/><p>Praktisch jede Interaktion mit einer Website oder Webapplikation kann gescriptet, d.h. automatisiert werden. Das Abgrasen von Webseiten nach bestimmten Informationen wird auch als <a href="http://en.wikipedia.org/wiki/Web_scraping">Scraping</a> bezeichnet(für die nicht menschlichen Besucher dieser Seite sei das erwähnt *g*) Scripte können einem eine ganze Menge Arbeit abnehmen und sogar Dinge tun, die manuell unmöglich wären. Ich beschäftige mich mit dem Thema schon seit einer ganzen Weile und möchte hier nun die von mir favorisierte Methode dafür vorstellen, die Bibliothek <a href="http://mechanize.rubyforge.org/mechanize/">Mechanize</a> für die Scriptsprache <a href="http://www.ruby-lang.org/en/">Ruby</a>.</p>
<p>Mechanize hat seinen <a href="http://search.cpan.org/dist/WWW-Mechanize/">Ursprung</a> in Perl, mittlerweile gibt es jedoch auch Implementierungen der API für <a href="http://wwwsearch.sourceforge.net/mechanize/">Python</a> und eben Ruby. Für PHP gibt es mit <a href="http://sourceforge.net/projects/snoopy/">Snoopy</a> ein ähnliches Projekt, wenn es auch bei weitem nicht so fortgeschritten ist. Mechanize bietet die Möglichkeit mit einfachen Methoden eine art Webbrowser zu simulieren. Alle Beispiele wurden mit Mechanize Version 0.9.2 und Ruby 1.8.7 getestet.</p>
<p><strong>Installation / Initialisierung</strong></p>
<p>Mechanize kann mit <a href="http://rubygems.org/">Gems</a>(ähnlich CPAN oder PEAR) installiert werden(<code># gem install mechanize --remote</code>), einige Distributionen bieten aber auch eigene Pakete an. Ein Ruby-Script kann daraufhin Mechanize inkludieren und ein Objekt erstellen:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span> <span style="color:#008000; font-style:italic;"># ist unter Umständen notwendig</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mechanize'</span>
agent = <span style="color:#6666ff; font-weight:bold;">WWW::Mechanize</span>.<span style="color:#9900CC;">new</span></pre></div></div>

<p>Jetzt ist Mechanize einsatzbereit, die folgenden Beispiele bauen darauf auf. Außerdem können mit dem <code>agent</code> nun noch grundlegende Einstellungen vorgenommen werden:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">agent.<span style="color:#9900CC;">set_proxy</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'localhost'</span>, <span style="color:#996600;">'8000'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
agent.<span style="color:#9900CC;">user_agent</span> = <span style="color:#996600;">'Individueller User-Agent'</span>
agent.<span style="color:#9900CC;">user_agent_alias</span> = <span style="color:#996600;">'Linux Mozilla'</span></pre></div></div>

<p>Die Einstellung &#8216;<code>user_agent_alias</code>&#8216; wählt einen User-Agent String aus dem folgenden Satz von Beispielen aus: Windows IE 6, Windows IE 7, Windows Mozilla, Mac Safari, Mac FireFox, Mac Mozilla, Linux Mozilla, Linux Konqueror, iPhone und Mechanize. Die Timing Einstellungen können ebenfalls sehr wichtig sein:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">agent.<span style="color:#9900CC;">open_timeout</span> = <span style="color:#006666;">3</span> <span style="color:#008000; font-style:italic;"># setzt timeouts</span>
agent.<span style="color:#9900CC;">read_timeout</span> = <span style="color:#006666;">4</span>
agent.<span style="color:#9900CC;">keep_alive</span> = <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#008000; font-style:italic;"># default ist true</span></pre></div></div>

<p>Hier folgen nun einige Beispiele, vielleicht werde ich mit der Zeit auch noch ein paar ergänzen, falls jemand Vorschläge hat, immer her damit. Ich habe auf <a href="http://apoc.sixserv.org/requestinfo">http://apoc.sixserv.org/requestinfo</a> ein kleines Skript am laufen das nützliche Informationen zum HTTP-Request liefert, das kann zum Experimentieren mit Mechanize sehr nützlich sein. Einige Beispiele findet man auch in den <a href="http://mechanize.rubyforge.org/mechanize/GUIDE_rdoc.html">GUIDE</a> und <a href="http://mechanize.rubyforge.org/mechanize/EXAMPLES_rdoc.html">EXAMPLES</a> Dateien des Mechanize Pakets. <span id="more-398"></span></p>
<p><strong>#01 &#8211; Manuelle Get Requests</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">url = <span style="color:#996600;">'http://apoc.sixserv.org/requestinfo/'</span>
page = agent.<span style="color:#9900CC;">get</span> url <span style="color:#008000; font-style:italic;"># einfacher get request</span>
<span style="color:#008000; font-style:italic;"># parameter können auch als Hash mitgeliefert werden:</span>
page = agent.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span>url, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;name&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;value&quot;</span>, <span style="color:#996600;">&quot;key&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;val&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p><strong>#02 &#8211; Manuelle Post Requests</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">url = <span style="color:#996600;">'http://apoc.sixserv.org/requestinfo/'</span>
page = agent.<span style="color:#9900CC;">post</span><span style="color:#006600; font-weight:bold;">&#40;</span>url, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;name&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;value&quot;</span>, <span style="color:#996600;">&quot;key&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;val&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>In diesem Fall werden die Post-Daten manuell erzeugt, häufig ist es jedoch besser das eigentliche Formular zu verwenden. z.B. ein Login oder Suchformular:</p>
<p><strong>#03 &#8211; Post Requests(/Submits) aus Formular erzeugen</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'https://twitter.com/login'</span>
login_form = page.<span style="color:#9900CC;">form_with</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'https://twitter.com/sessions'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
login_form<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'session[username_or_email]'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'[Username]'</span>
login_form<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'session[password]'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'[Password]'</span>
page = agent.<span style="color:#9900CC;">submit</span> login_form</pre></div></div>

<p>Dieses kleine Beispiel loggt sich bei Twitter ein.</p>
<p><strong>#04 &#8211; Navigation über Links und der History</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'http://www.heise.de/'</span>
page = agent.<span style="color:#9900CC;">click</span><span style="color:#006600; font-weight:bold;">&#40;</span>page.<span style="color:#9900CC;">link_with</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">/</span>Telepolis<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
page = agent.<span style="color:#9900CC;">click</span><span style="color:#006600; font-weight:bold;">&#40;</span>page.<span style="color:#9900CC;">link_with</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:href</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">/</span>artikel<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
agent.<span style="color:#9900CC;">back</span>
agent.<span style="color:#9900CC;">back</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> page.<span style="color:#9900CC;">body</span></pre></div></div>

<p><strong>#05 &#8211; Exceptions</strong></p>
<p>Wird z.B. eine nicht vorhandene Seite aufgerufen, wirft Mechanize eine Exception, diese sollte Abgefangen werden da das Skript sonst an dieser Stelle abbricht.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">begin</span>
  page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'http://apoc.sixserv.org/diese/seite/gibt/es/nicht/'</span>
<span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">WWW::Mechanize::ResponseCodeError</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;ResponseCodeError - Code: #{$!}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong>#06 &#8211; Referer</strong></p>
<p>Mechanize benutzt seine Navigations-History auch zum senden eines passenden Referers, diesen kann man allerdings auch manuell bestimmen, hier für GET und POST Requests:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://apoc.sixserv.org/requestinfo/'</span>,
<span style="color:#ff3333; font-weight:bold;">:referer</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://google.com/this/is/a/custom/referer'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> page.<span style="color:#9900CC;">body</span></pre></div></div>

<p><strong>#07 &#8211; Headers / HEAD / Redirect</strong></p>
<p>Der Request Header kann manuell erweitert werden, bspw. den &#8220;X-Requested-With&#8221; so zu setzen um einen Ajax-Aufruf zu simulieren. Es gibt leider keine wirklich gute Möglichkeit eigene Header zu setzen, das ganze wurde in den vergangenen Versionen oft geändert, die folgende Methode funktioniert mit älteren Versionen von Mechanize nicht.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">agent.<span style="color:#9900CC;">pre_connect_hooks</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>params<span style="color:#006600; font-weight:bold;">|</span>
  params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:request</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'X-Requested-With'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'XMLHttpRequest'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Dies ist übrigens auch die einzig wirklich einfache Möglichkeit manuell Cookies zu setzen/ zu übertragen. Die andere Seite, also das Abfragen des Response-Headers ist trivial, hier wird z.B. per HEAD-Request die Server und evtl. der PHP-Version abgefragt:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">head</span> <span style="color:#996600;">'http://sixserv.org'</span>
server_version = page.<span style="color:#9900CC;">header</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'server'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Server: #{server_version}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> page.<span style="color:#9900CC;">header</span>.<span style="color:#9900CC;">key</span>? <span style="color:#996600;">'x-powered-by'</span>
  php_version = page.<span style="color:#9900CC;">header</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'x-powered-by'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;X-Powered-By: #{php_version}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Will man einen HTTP-Redirect(302) abfragen, muss man zunächst das automatische Weiterleiten im Mechanize deaktivieren, dann kann man den Zielort abfragen ohne das Ziel zu besuchen. (Nützlich z.B. wenn man auf einen Datei-Download weitergeleitet wird)</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">agent.<span style="color:#9900CC;">redirect_ok</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'http://www.sixserv.org/'</span> <span style="color:#008000; font-style:italic;"># leitet auf http://sixserv.org weiter</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> page.<span style="color:#9900CC;">header</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'location'</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p><strong>#08 &#8211; Threads</strong></p>
<p>Will man ein Script etwas beschleunigen kann man mit Threads arbeiten um mehrere Inhalte gleichzeitig abzufragen. Das folgende Grundgerüst für z.B. Foren mit vielen Seiten, bearbeitet 10 Seiten gleichzeitig und kann so ein Skript erheblich beschleunigen. In diesem Fall würde man allerdings wahrscheinlich eher nicht Mechanize einsetzen sondern eine Low-Level API fürs Netzwerk-Zugriff, das wäre deutlich effektiver ich will nur das Prinzip deutlich machen.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#ff6633; font-weight:bold;">$threads</span> = <span style="color:#006666;">0</span>
last_page = <span style="color:#006666;">200</span>
max_threads = <span style="color:#006666;">10</span>
<span style="color:#9966CC; font-weight:bold;">for</span> page <span style="color:#9966CC; font-weight:bold;">in</span> 1..<span style="color:#9900CC;">last_page</span>
  <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>page<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>page<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#ff6633; font-weight:bold;">$threads</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; [*] Create new Thread for scanning Page ##{page}&quot;</span>
&nbsp;
    page = <span style="color:#ff6633; font-weight:bold;">$agent</span>.<span style="color:#9900CC;">get</span> <span style="color:#996600;">&quot;http://example.com/datalist.php?page=#{page}&quot;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># process / save received data</span>
&nbsp;
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; [*] Destroy Thread&quot;</span>
    <span style="color:#ff6633; font-weight:bold;">$threads</span> <span style="color:#006600; font-weight:bold;">-</span>= <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#ff6633; font-weight:bold;">$threads</span> <span style="color:#006600; font-weight:bold;">&gt;</span> max_threads
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; [*] Next Page&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong>#09 &#8211; Parsing / XPath</strong></p>
<p>Hat man eine Seite empfangen und will bestimmte Inhalte herausfiltern gibt es mehrere Möglichkeiten, meistens werden mehr oder weniger umfangreiche Reguläre Ausdrücke zum parsen verwendet:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'http://example.com/'</span>
page.<span style="color:#9900CC;">body</span>.<span style="color:#9900CC;">match</span> <span style="color:#006600; font-weight:bold;">/&lt;</span> h3<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>^<span style="color:#006600; font-weight:bold;">&lt;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&lt;</span> \<span style="color:#006600; font-weight:bold;">/</span>h3<span style="color:#006600; font-weight:bold;">&gt;/</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Überschrift 3: #{$1}&quot;</span></pre></div></div>

<p>Wesentlich stabiler sind jedoch <a href="http://en.wikipedia.org/wiki/XPath">XPath</a> Ausdrücke die sich direkt auf die Struktur eines XML-Dokuments beziehen. Werden nur Kleinigkeiten am HTML-Code geändert sind RegEx&#8217;s häuft aufgeschmissen und müssen mühsam angepasst werden, XPath ausdrücke dagegen funktionieren häufig auch dann noch. Eine Einführung gibt es z.B. auf <a href="http://www.w3schools.com/XPath/default.asp">w3schools.com</a> oder der offizielle <a href="http://www.w3.org/TR/xpath">Standard der W3C</a>. Allerdings geht das alles auch sehr einfach mit entsprechenden Werkzeugen, so gibt es z.B. eine Menge Firefox-Addons(nützlich ist z.B. der <a href="https://addons.mozilla.org/en-US/firefox/addon/1095">XPath Checker</a>) die einem, zu markierten Elementen einer Seite einen passenden XPath Ausdruck anzeigt oder umgekehrt, wenn auch die nur einfachen Ausdrücke gelegentlich Nachbearbeitung erfordern.</p>
<div id="attachment_427" class="wp-caption aligncenter" style="width: 690px"><img class="size-full wp-image-427" title="XPath mit Firebug" src="http://sixserv.org/wp-content/uploads/2009/05/xpath_mit_firebug_small.png" alt="XPath mit Firebug" width="680" height="585" /><p class="wp-caption-text">Firebug kann einem zu beliebigen Elementen einer Seite einen passenden XPath(Rot unterstrichen) anzeigen.</p></div>
<p>Der von Mechanize verwendete HTML-Parser <a href="http://nokogiri.rubyforge.org/nokogiri/">Nokogiri</a>(鋸) ist sehr robust und kann auch &#8220;kaputtes&#8221; HTML parsen, was XPath zu einem auserordentlich mächtigen Werkzeug macht, um jede beliebige Information aus noch so komplexen Seiten zu extrahieren.</p>
<p>Mit <a href="https://addons.mozilla.org/en-US/firefox/addon/1843">Firebug</a>: Ein beliebiges Element auf einer Seite markieren und im Kontextmenü, &#8220;Inspect Element&#8221; aufrufen. In der oberen Zeile von Firebug(siehe Screenshot) kann im Kontextmenü mit &#8220;Copy XPath&#8221; der XPath geholt werden. Im Beispiel wird der XPath zum Comic-Strip auf <a href="http://xkcd.com/">XKCD</a> angezeigt: <code>/html/body/div/div[2]/div/div[2]/div/div/img</code> In Mechanize kann so der IMG-Tag extrahiert werden:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">page = agent.<span style="color:#9900CC;">get</span> <span style="color:#996600;">'http://xkcd.com/'</span>
img = page.<span style="color:#9900CC;">search</span> <span style="color:#996600;">'/html/body/div/div[2]/div/div[2]/div/div/img'</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> img
<span style="color:#008000; font-style:italic;"># Ausgabe: &lt; img src=&quot;http://imgs.xkcd.com/comics/designated_drivers.png&quot; title=&quot;Calling a cab means cutting into beer money.&quot; alt=&quot;Designated Drivers&quot; &gt;</span></pre></div></div>

<p><em>Das wars damit vorerst. Anmerkungen oder Fragen zu den Beispielen oder Ideen für weitere, bitte in den Kommentaren hinterlassen.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://sixserv.org/2009/05/27/webscripting-mit-ruby-und-mechanize/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
