<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: mechanize code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 12:17:34 GMT</pubDate>
    <description>DZone Snippets: mechanize code</description>
    <item>
      <title>Getting Started With WWW::Mechanize</title>
      <link>http://snippets.dzone.com/posts/show/5134</link>
      <description>This Ruby code uses WWW:mechanize to act like a web browser.  &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; require 'rubygems'&lt;br /&gt; require 'mechanize'&lt;br /&gt;&lt;br /&gt; agent = WWW::Mechanize.new&lt;br /&gt; page = agent.get('http://google.com/')&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Refer to the documentation at http://mechanize.rubyforge.org/mechanize/. Then gem install mechanize, and try running the code in an irb session.&lt;br /&gt;&lt;br /&gt;output (extract):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;=&gt; #&lt;WWW::Mechanize::Page&lt;br /&gt; {url #&lt;URI::HTTP:0xfdbbbb286 URL:http://www.google.com/&gt;}&lt;br /&gt; {meta}&lt;br /&gt; {title "Google"}&lt;br /&gt; {iframes}&lt;br /&gt; {frames}&lt;br /&gt; {links&lt;br /&gt;  #&lt;WWW::Mechanize::Page::Link&lt;br /&gt;   "Images"&lt;br /&gt;   "http://images.google.com/imghp?hl=en&amp;tab=wi"&gt;&lt;br /&gt;  #&lt;WWW::Mechanize::Page::Link&lt;br /&gt;   "Maps"&lt;br /&gt;   "http://maps.google.com/maps?hl=en&amp;tab=wl"&gt;&lt;br /&gt;  #&lt;WWW::Mechanize::Page::Link&lt;br /&gt;   "News"&lt;br /&gt;   "http://news.google.com/nwshp?hl=en&amp;tab=wn"&gt;&lt;br /&gt;  #&lt;WWW::Mechanize::Page::Link&lt;br /&gt;   "Shopping"&lt;br /&gt;   "http://www.google.com/prdhp?hl=en&amp;tab=wf"&gt;&lt;br /&gt;  #&lt;WWW::Mechanize::Page::Link&lt;br /&gt;   "Gmail"&lt;br /&gt;   "http://mail.google.com/mail/?hl=en&amp;tab=wm"&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 12 Feb 2008 17:53:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5134</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Automatic Bank Balance Checking at WaMu</title>
      <link>http://snippets.dzone.com/posts/show/4548</link>
      <description>I bank at Washington Mutual, and I wanted to see when I was spending too much, or get automated updates of what I was spending money on each day and what my balance was.  However, it's really difficult to do and WaMu won't let you use an API or something helpful like that, so I wrote my own little screen-scraper to do it.&lt;br /&gt;&lt;br /&gt;This requires WWW::Mechanize, but it works quite well.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'mechanize'&lt;br /&gt;&lt;br /&gt;agent = WWW::Mechanize.new&lt;br /&gt;&lt;br /&gt;# login to the account&lt;br /&gt;puts "Login"&lt;br /&gt;page = agent.get('https://online.wamu.com/IdentityManagement/Logon.aspx')&lt;br /&gt;login_form = page.forms.with.name("frmLogin").first&lt;br /&gt;login_form.txtUserID = '&lt;UNAME&gt;'&lt;br /&gt;login_form.password = '&lt;PWD&gt;'&lt;br /&gt;page = agent.submit(login_form, login_form.buttons.first)&lt;br /&gt;&lt;br /&gt;# click on the download button&lt;br /&gt;puts "Download"&lt;br /&gt;dl_link = page.links.with.text(/WAMU FREE CHECKING/)&lt;br /&gt;page = agent.click(dl_link)&lt;br /&gt;&lt;br /&gt;line_items = []&lt;br /&gt;&lt;br /&gt;trs = (page/"table#_ctl0_depositTransactionsGrid/tr")&lt;br /&gt;trs.shift&lt;br /&gt;trs.each do |tr|&lt;br /&gt;  tds = (tr/:td)&lt;br /&gt;&lt;br /&gt;  dtd = tds[1].inner_html&lt;br /&gt;  js_call = dtd.match(/showDDATransactionDetails\('(.*)'\);/)[1]&lt;br /&gt;  js_fields = js_call.split("','")&lt;br /&gt;&lt;br /&gt;  item = {}&lt;br /&gt;  item['type'] = js_fields[1]&lt;br /&gt;  item['descr'] = js_fields[3]&lt;br /&gt;  item['amount'] = js_fields[4]&lt;br /&gt;  item['tranid'] = js_fields[6]&lt;br /&gt;  # if !tranid = ovedraft charge / bank fee&lt;br /&gt;&lt;br /&gt;  item['date'] = tds[0].inner_html&lt;br /&gt;  item['debit'] = tds[3].inner_html&lt;br /&gt;  item['credit'] = tds[4].inner_html&lt;br /&gt;  item['balance'] = tds[5].inner_html&lt;br /&gt;&lt;br /&gt;  line_items &lt;&lt; item&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;pp line_items.first&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Sun, 16 Sep 2007 21:18:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4548</guid>
      <author>schacon (Scott Chacon)</author>
    </item>
  </channel>
</rss>
