<?xml version="1.0" encoding="utf-8"?>
<feed
    xmlns="http://www.w3.org/2005/Atom"
    xmlns:at="http://www.sixapart.com/ns/at"
    xmlns:icbm="http://postneo.com/icbm"
    xmlns:rvw="http://purl.org/NET/RVW/0.2/"
    xml:lang="en">
    <title>バイトのメモなど</title>
    <link rel="self" type="application/atom+xml" title="バイトのメモなど (Atom)" href="http://takumi.vox.com/library/posts/2007/01/page/1/atom.xml" />
    <link rel="alternate" type="text/html" title="バイトのメモなど" href="http://takumi.vox.com/library/posts/2007/01/page/1/"/> 
    <link rel="service.post" type="application/atom+xml" title="バイトのメモなど" href="http://www.vox.com/services/atom/svc=post/collection_id=6a00c22522e8d9604a00c22523dff18e1d" /> 
    <link rel="service.subscribe" type="application/atom+xml" title="バイトのメモなど" href="http://takumi.vox.com/library/posts/2007/01/atom.xml" />   
    <link rel="last" type="application/atom+xml" title="バイトのメモなど" href="http://takumi.vox.com/library/posts/2007/01/page/1/atom.xml" />  
    <generator uri="http://www.vox.com/">Vox</generator>
    <updated>2007-01-31T13:29:54Z</updated> 
    <author>
        <name>takumi</name>
        <uri>http://takumi.vox.com/?_c=feed-atom-full</uri>
    </author> 
    <id>tag:vox.com,2006:6p00c22522e8d9604a/2007/01/</id> 
    <subtitle>あと、買った本とか</subtitle>  
    
    <entry>
        <title>to_absolute_uriをＶB６で</title>   
        <link rel="alternate" type="text/html" title="to_absolute_uriをＶB６で" href="http://takumi.vox.com/library/post/to_absolute_uri%E3%82%92%EF%BD%82%EF%BD%96%EF%BC%96%E3%81%A7.html?_c=feed-atom-full" />  
        <link rel="service.post" type="application/atom+xml" title="to_absolute_uriをＶB６で" href="http://takumi.vox.com/library/post/to_absolute_uri%E3%82%92%EF%BD%82%EF%BD%96%EF%BC%96%E3%81%A7.html?_c=feed-atom-full#comments" /> 
        <link rel="service.edit" type="application/atom+xml" title="to_absolute_uriをＶB６で" href="http://www.vox.com/atom/svc=post/asset_id=6a00c22522e8d9604a00cdf7f3e09d094f" />          <id>tag:vox.com,2007-01-15:asset-6a00c22522e8d9604a00cdf7f3e09d094f</id>
        <published>2007-01-15T08:25:32Z</published>
        <updated>2007-01-31T13:29:54Z</updated>
    
        <author>
            <name>takumi</name>
            <uri>http://takumi.vox.com/?_c=feed-atom-full</uri>
        </author>
    
        
        <content type="html" xml:base="http://takumi.vox.com/?_c=feed-atom-full">
            <![CDATA[
                <div xmlns="http://www.w3.org/1999/xhtml" xmlns:at="http://www.sixapart.com/ns/at">
        <pre>VB6で下のコードを動かすと、getメソッドが無いって怒られる。<br />既存のWWW::Mechanizeに定義を追加するのではなくて、新しいWWW::Mechanizeを作り出している。<br /><pre><br />require &quot;rubygems&quot;<br />require &quot;mechanize&quot;<br /><br />module WWW<br />&#160;&#160; class Mechanize<br />&#160;&#160;&#160;&#160;&#160; def to_absolute_uri(url, cur_page=current_page())<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; url =&#160; URI.parse(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Util.html_unescape(url.to_s.strip)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ) unless url.is_a? URI<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; # construct an absolute uri<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if url.relative?<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; raise &#39;no history. please specify an absolute URL&#39; unless cur_page.uri<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; url = cur_page.uri + url<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; end<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return url<br />&#160;&#160;&#160;&#160;&#160; end<br />&#160;&#160; end<br />end<br />agent = WWW::Mechanize.new<br />agent.user_agent_alias=&#39;Windows Mozilla&#39;<br />puts agent.get(&quot;http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8&quot;).body<br /></pre> <br />で、<br /><pre><br />module WWW<br />   class Mechanize<br />   end<br />end<br /></pre><br />を<br /><pre><br />class WWW::Mechanize<br />end<br /></pre><br />に変更したら動いた。何でだ？<br /></pre>    <p style="clear:both;"> 
    <a href="http://takumi.vox.com/library/post/to_absolute_uri%E3%82%92%EF%BD%82%EF%BD%96%EF%BC%96%E3%81%A7.html?_c=feed-atom-full#comments">Read and post comments</a>   |   
    <a href="http://www.vox.com/share/6a00c22522e8d9604a00cdf7f3e09d094f?_c=feed-atom-full">Send to a friend</a> 
</p>

                </div>
            ]]>
        </content> 
    </entry> 
    
    <entry>
        <title>to_absolute_uri</title>   
        <link rel="alternate" type="text/html" title="to_absolute_uri" href="http://takumi.vox.com/library/post/to_absolute_uri.html?_c=feed-atom-full" />  
        <link rel="service.post" type="application/atom+xml" title="to_absolute_uri" href="http://takumi.vox.com/library/post/to_absolute_uri.html?_c=feed-atom-full#comments" /> 
        <link rel="service.edit" type="application/atom+xml" title="to_absolute_uri" href="http://www.vox.com/atom/svc=post/asset_id=6a00c22522e8d9604a00d09e4ca563be2b" />          <id>tag:vox.com,2007-01-12:asset-6a00c22522e8d9604a00d09e4ca563be2b</id>
        <published>2007-01-12T09:14:36Z</published>
        <updated>2007-01-12T10:46:01Z</updated>
    
        <author>
            <name>takumi</name>
            <uri>http://takumi.vox.com/?_c=feed-atom-full</uri>
        </author>
    
        
        <content type="html" xml:base="http://takumi.vox.com/?_c=feed-atom-full">
            <![CDATA[
                <div xmlns="http://www.w3.org/1999/xhtml" xmlns:at="http://www.sixapart.com/ns/at">
        <p>rubyのmechanizeで日本語が通らない。<br /><pre><br /># sample.rb<br />require &quot;rubygems&quot;<br />require &quot;mechanize&quot;<br /><br />agent = WWW::Mechanize.new<br />agent.user_agent_alias=&#39;Windows Mozilla&#39;<br />agent.get(&quot;http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8&quot;)<br /></pre><br />これを実行すると<br /><pre><br />&gt;ruby sample.rb<br />ruby sample.rb<br />C:/opt/ruby-1.8/lib/ruby/1.8/uri/common.rb:432:in `split&#39;: bad URI(is not URI?): http://ja.wikipedia.org/wiki/繝｡繧､繝ｳ繝壹・繧ｸ (URI::InvalidURIError)<br />&#160;&#160;&#160; from C:/opt/ruby-1.8/lib/ruby/1.8/uri/common.rb:481:in `parse&#39;<br />&#160;&#160;&#160; from C:/opt/ruby-1.8/lib/ruby/gems/1.8/gems/mechanize-0.6.4/lib/mechanize.rb:272:in `to_absolute_uri&#39;<br />&#160;&#160;&#160; from C:/opt/ruby-1.8/lib/ruby/gems/1.8/gems/mechanize-0.6.4/lib/mechanize.rb:141:in `get&#39;<br />&#160;&#160;&#160; from sample.rb:6<br /></pre><br />例外はmechanize.rbのto_absolute_uriというメソッドから。<br /><pre><br />#コード<br />&#160; def to_absolute_uri(url, cur_page=current_page())<br />&#160;&#160;&#160; url =&#160; URI.parse(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; URI.unescape(Util.html_unescape(url.to_s.strip)).gsub(/ /, &#39;%20&#39;)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ) unless url.is_a? URI<br /><br />&#160;&#160;&#160; # construct an absolute uri<br />&#160;&#160;&#160; if url.relative?<br />&#160;&#160;&#160;&#160;&#160; raise &#39;no history. please specify an absolute URL&#39; unless cur_page.uri<br />&#160;&#160;&#160;&#160;&#160; url = cur_page.uri + url<br />&#160;&#160;&#160; end<br /><br />&#160;&#160;&#160; return url<br />&#160; end<br /></pre></p><p>urlを<br />url　→　htmlでunescapeしたurl　→　htmlでunescapeしたurlをuriでunescapeしたurl<br />→　htmlでunescapeしたurlをuriでunescapeしたurlの空白文字を%20に置換したurl<br />と変更を繰り返してる。</p><p>ところで、なぜいったんURI.unescapeしたあと空白文字を%20に置換しているんだ？<br />これなら、<br />url　→　htmlでunescapeしたurl<br />で、いいはずなのに。</p><p>実際にto_absolute_uriのurlの部分を<br /><pre><br />&#160;&#160;&#160; url =&#160; URI.parse(<br />           URI.unescape(Util.html_unescape(url.to_s.strip)).gsub(/ /, &#39;%20&#39;)<br />                     ) unless url.is_a? URI<br /></pre><br />から<br /><pre><br />&#160;&#160;&#160; url =&#160; URI.parse(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Util.html_unescape(url.to_s.strip)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ) unless url.is_a? URI<br /></pre><br />を変更すると、上のsample.rbが動作する。</p><p>Mechanizeのメーリングリストを見てみると、このto_absolute_uriって変更要望がたくさん出ているんだよなぁ。。。</p><p>追記<br />既存のものを再定義すればいいわけだから、これでとりあえずしのげた。<br /><pre><br />require &quot;rubygems&quot;<br />require &quot;mechanize&quot;<br /><br />module WWW<br />&#160;&#160; class Mechanize<br />&#160;&#160;&#160;&#160;&#160; def to_absolute_uri(url, cur_page=current_page())<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; url =&#160; URI.parse(<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Util.html_unescape(url.to_s.strip)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ) unless url.is_a? URI<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; # construct an absolute uri<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if url.relative?<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; raise &#39;no history. please specify an absolute URL&#39; unless cur_page.uri<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; url = cur_page.uri + url<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; end<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return url<br />&#160;&#160;&#160;&#160;&#160; end<br />&#160;&#160; end<br />end<br />agent = WWW::Mechanize.new<br />agent.user_agent_alias=&#39;Windows Mozilla&#39;<br />puts agent.get(&quot;http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8&quot;).body<br /> </pre></p>   <p style="clear:both;"> 
    <a href="http://takumi.vox.com/library/post/to_absolute_uri.html?_c=feed-atom-full#comments">Read and post comments</a>   |   
    <a href="http://www.vox.com/share/6a00c22522e8d9604a00d09e4ca563be2b?_c=feed-atom-full">Send to a friend</a> 
</p>

                </div>
            ]]>
        </content> 
    </entry> 
</feed>


