<?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>acloudtree &#187; Scraping</title>
	<atom:link href="http://www.acloudtree.com/category/scraping/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.acloudtree.com</link>
	<description>Programming, Computers, Writing, Economics, and Life</description>
	<lastBuildDate>Tue, 07 Feb 2012 00:03:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Newbs, Ruby, Profiling Memory Leaks Using Memprof, And Web Scraping with Mechanize</title>
		<link>http://www.acloudtree.com/newbs-ruby-profiling-memory-leaks-using-memprof-and-web-scraping-with-mechanize/</link>
		<comments>http://www.acloudtree.com/newbs-ruby-profiling-memory-leaks-using-memprof-and-web-scraping-with-mechanize/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 21:57:49 +0000</pubDate>
		<dc:creator>jared.folkins</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[REGEX]]></category>
		<category><![CDATA[Scraping]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Crawler]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[Mechanize]]></category>
		<category><![CDATA[Newb]]></category>
		<category><![CDATA[Profile]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scrape]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.acloudtree.com/?p=1007</guid>
		<description><![CDATA[So for the past four weeks in my spare time, I&#8217;ve been writing a sweet web scraping application for a personal research project. After overcoming the majority of regex and pattern matching challenges, I was finally able to get my spider a crawlin&#8217; and then go to bed. When I woke, I was excited to [...]]]></description>
			<content:encoded><![CDATA[<p>So for the past four weeks in my spare time, I&#8217;ve been writing a sweet web scraping application for a personal research project. After overcoming the majority of regex and pattern matching challenges, I was finally able to get my spider a crawlin&#8217; and then go to bed.</p>
<p>When I woke, I was excited to see what little bits of interesting information my application had found. But instead, I saw that it had dumped stack. And as a Ruby newb, here starts the long and arduous journey to understanding how to profile a ruby application. Yikes!</p>
<h2>Definition of &#8220;Memory Leak&#8221; in Ruby</h2>
<p>From what I have researched and my own personal experience. You (the developer) can&#8217;t really create a traditional memory leak.</p>
<h4>Traditional Leak</h4>
<ol>
<li>Allocate Memory (malloc, alloc)</li>
<li>Assign Value (Object, string, int, etc&#8230;) To Memory</li>
<li>No longer needing the value, you forget to release (dealloc) it</li>
<li>The data stays in memory during the life of your application</li>
<li>Particularly problematic when iterating or looping and assigning new values to memory</li>
</ol>
<h4>Ruby Memory Leak</h4>
<ol>
<li>You assign a value to a variable</li>
<li>You expect that the value has fallen out of scope</li>
<li>Instead, it persists in memory as some Object you are unaware of is retaining it</li>
<li>Not really noticeable in small applications or page requests where memory is freed at the end of the run cycle</li>
</ol>
<p>Thats the basic difference (from my understanding). Please correct me if I am off on something.</p>
<h2>Tips To Identify That You Have A Memory Leak</h2>
<p>So a couple quick ways that you can tell you <span style="text-decoration: underline;">could</span> have a memory leak.</p>
<h4>Total Object Count</h4>
<p>Try printing the total Object count to screen while your application is running. Should the count increase over time, this would indicate that you are retaining objects. Please note that you need to force Garbage Collection regularly to make sure that the total Object count is not just the logical growth cycle of your application.</p>
<h4>Monitor Memory Usage</h4>
<p>Monitor the Process I.D. (PID) of your application and look to see if memory usage increases over a given period of time. Should it continue to go up, this would strengthen the argument that you have a leak.</p>
<p>Again, these are two quick methods I used to come to the following point of action.</p>
<p>&#8220;Crap, it looks like something is going on with memory usage, I better take a closer look.&#8221;</p>
<h2>Try forcing your Objects to release</h2>
<p>I learned rather quickly, that by design, Ruby does not allow you to release or unset your Objects manually. So there ya go.</p>
<h2>Set your Objects to <em>nil</em> to identify they are ready to be Garbage Collected</h2>
<p>I tried this while grasping at straws, it was a dumb idea. I was taking shots in the dark and I wouldn&#8217;t advise going this route. What I needed was a really deep look at what Objects I was storing in memory so I could develop a story on why my application was leaking.</p>
<h2>Memprof &amp; its visualization partner Memprof.com</h2>
<p>This is when I happened across the <a title="memprof git repo" href="https://github.com/ice799/memprof" target="_blank">git repo Memprof</a> by Joe Damato (@joedamato <a href="http://timetobleed.com/">http://timetobleed.com</a>). It was exactly what I needed.</p>
<h4>Using Ruby 1.8.7, Install Memprof</h4>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ gem <span style="color: #c20cb9; font-weight: bold;">install</span> memprof</pre></div></div>

<h4>Signup for memprof.com</h4>
<p>memprof.com (be aware, that when using this tool, it publishes your code to the web)</p>
<p><strong>Include Memprof</strong> (at the <span style="text-decoration: underline;">VERY</span> beginning of your application, just in case you want to profile your included gems too)</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;">`gem which memprof/signal`</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span></pre></div></div>

<h4>Wrap Run Loop in Memprof.trace</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> run_loop
  <span style="color:#9966CC; font-weight:bold;">while</span> next_link?<span style="color:#006600; font-weight:bold;">&#40;</span>@browser.<span style="color:#9900CC;">page</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    Memprof.<span style="color:#9900CC;">trace</span><span style="color:#006600; font-weight:bold;">&#123;</span>
      iterate_search_page<span style="color:#006600; font-weight:bold;">&#40;</span>@browser.<span style="color:#9900CC;">page</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      click_next_link<span style="color:#006600; font-weight:bold;">&#40;</span>@browser.<span style="color:#9900CC;">page</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h4>Run Your Application</h4>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ ruby web_crawler.rb</pre></div></div>

<h4>Find Your Process ID of application</h4>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-ef</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> web_crawler.rb</pre></div></div>

<h4>After 5 minutes, Dump Trace To Memprof.com</h4>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ memprof <span style="color: #660033;">--pid</span> <span style="color: #000000;">27890</span> <span style="color: #660033;">--name</span> elvis_dump2 <span style="color: #660033;">--key</span> YOUR_API_KEY</pre></div></div>

<h4>Analyze the heap visually with Memprof.com</h4>
<p style="text-align: center;"><a href="http://www.acloudtree.com/wp-content/uploads/2011/08/dump.png"><img class="size-medium wp-image-1040 aligncenter" title="dump" src="http://www.acloudtree.com/wp-content/uploads/2011/08/dump-300x272.png" alt="" width="300" height="272" /></a></p>
<h4>Click On (objects with most outbound references)</h4>
<p><a href="http://www.acloudtree.com/wp-content/uploads/2011/08/outbound.png"><img class="aligncenter size-medium wp-image-1047" title="outbound" src="http://www.acloudtree.com/wp-content/uploads/2011/08/outbound-300x185.png" alt="" width="300" height="185" /></a></p>
<p>On the far right, you will see an Object with the &#8220;Mechanize&#8221; class, and its length is 1081. This piece of information was critical in identifying that a Mechanize object I had instantiated was retaining a crap pile of objects.</p>
<h3>What was the issue?</h3>
<p>So it turns out that by default, Mechanize does <span style="text-decoration: underline;">NOT</span> set a max_history=() value. What does this mean? Basically, my web crawler was storing every visited page, in memory, in entirety. BEWARE!</p>
<p>Hat tip ( <a href="http://twitter.com/bradhe">@bradhe</a>, <a href="http://twitter.com/tim_linquist">@tim_linquist</a>, <a href="http://twitter.com/joedamato">@joedamato</a>, <a href="http://twitter.com/elazar">@elazar</a>)</p>
<p><strong>Note:</strong> My &#8220;Ruby Memory Leaks&#8221; definition is not entirely correct, but for the newbs out there reading this, I think trying to break it down further could potentially confuse you. If you want to understand Ruby memory allocation at some point, go watch this video by Joe Damato. (<a title="Joe Damato Ruby Memory Allocation" href="http://www.viddler.com/explore/GreggPollack/videos/39/">link</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.acloudtree.com/newbs-ruby-profiling-memory-leaks-using-memprof-and-web-scraping-with-mechanize/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>(Nerd) Mechanize &amp; Javascript</title>
		<link>http://www.acloudtree.com/nerd-mechanize-javascript/</link>
		<comments>http://www.acloudtree.com/nerd-mechanize-javascript/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 17:02:04 +0000</pubDate>
		<dc:creator>jared.folkins</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[REGEX]]></category>
		<category><![CDATA[Scraping]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Mechanize]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.acloudtree.com/?p=101</guid>
		<description><![CDATA[This is from the mechanize site, wish I would have read it before I started. Since Javascript is completely visible to the client, it cannot be used to prevent a scraper from following links. But it can make life difficult, and until someone writes a Javascript interpreter for Perl or a Mechanize clone to control [...]]]></description>
			<content:encoded><![CDATA[<p>This is from the mechanize site, wish I would have read it before I started.</p>
<blockquote><p>Since Javascript is completely visible to the client, it cannot be used to prevent a scraper from following links. But it can make life difficult, and until someone writes a Javascript interpreter for Perl or a Mechanize clone to control Firefox, there will be no general solution. But if you want to scrape specific pages, then a solution is always possible.</p>
<p>One typical use of Javascript is to perform argument checking before posting to the server. The URL you want is probably just buried in the Javascript function. Do a regular expression match on $mech-&gt;content() to find the link that you want and $mech-&gt;get it directly (this assumes that you know what you are looking for in advance).</p>
<p>In more difficult cases, the Javascript is used for URL mangling to satisfy the needs of some middleware. In this case you need to figure out what the Javascript is doing (why are these URLs always really long?). There is probably some function with one or more arguments which calculates the new URL. Step one: using your favorite browser, get the before and after URLs and save them to files. Edit each file, converting the the argument separators (&#8216;?&#8217;, &#8216;&amp;&#8217; or &#8216;;&#8217;) into newlines. Now it is easy to use diff or comm to find out what Javascript did to the URL. Step 2 &#8211; find the function call which created the URL &#8211; you will need to parse and interpret its argument list. Using the Javascript Debugger Extension for Firefox may help with the analysis. At this point, it is fairly trivial to write your own function which emulates the Javascript for the pages you want to process.</p>
<p>Here&#8217;s annother approach that answers the question, &#8220;It works in Firefox, but why not Mech?&#8221; Everything the web server knows about the client is present in the HTTP request. If two requests are identical, the results should be identical. So the real question is &#8220;What is different between the mech request and the Firefox request?&#8221;</p>
<p>The Firefox extension &#8220;Tamper Data&#8221; is an effective tool for examining the headers of the requests to the server. Compare that with what LWP is sending. Once the two are identical, the action of the server should be the same as well.</p>
<p>I say &#8220;should&#8221;, because this is an oversimplification &#8211; some values are naturally unique, e.g. a SessionID, but if a SessionID is present, that is probably sufficient, even though the value will be different between the LWP request and the Firefox request. The server could use the session to store information which is troublesome, but that&#8217;s not the first place to look (and highly unlikely to be relevant when you are requesting the login page of your site).</p>
<p>Generally the problem is to be found in missing or incorrect POSTDATA arguments, Cookies, User-Agents, Accepts, etc. If you are using mech, then redirects and cookies should not be a problem, but are listed here for completeness. If you are missing headers, $mech-&gt;add_header can be used to add the headers that you need.</p></blockquote>
<p><a title="Mechanize" href="http://search.cpan.org/dist/WWW-Mechanize/lib/WWW/Mechanize/FAQ.pod#I_have_this_web_page_that_has_JavaScript_on_it,_and_my_Mech_program_doesn%27t_work." target="_blank">LINK</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.acloudtree.com/nerd-mechanize-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

