VocabReminder
Easily build a great vocabulary without studying! Now on the App Store!
Click here for more info about the app

How to Extract the Title From an HTML Page with Ruby

       

This snippet will make a request to this page and extract the title from the title tag.


require 'open-uri'

html = open('http://www.seanbehan.com/how-to-extract-the-title-from-an-html-page-with-ruby').read

title = html.match(/(.*)<\/title>/) { $1 }

puts title
</code></pre>

<p>The regular expression here matches everything between the title tags. Anything within the parens "(.*)" is kept around as variable which you can access in the block with variables $1, $2, $3... $n, depending on how many matches are found.</p>
  </div>

  
  
  <div class="" style="max-width:800px;margin:0px auto;padding:10px 25px 50px;">Tagged w/
    
      
        <a style="padding:2px;background:lightyellow;border-radius:2px;box-shadow:1px 1px 2px #CCC;" href="/tags/ruby">#ruby</a>
      
    
      
        <a style="padding:2px;background:lightyellow;border-radius:2px;box-shadow:1px 1px 2px #CCC;" href="/tags/regular%20expressions">#regular expressions</a>
      
    
  </div>

  

  <br>

  <!-- <iframe src="//rcm-na.amazon-adsystem.com/e/cm?o=1&p=12&l=ur1&category=books&banner=1MH7120YTP434ANM2F82&f=ifr&linkID=5e4865fe502f81cb376451e349a8092c&t=andthings-20&tracking_id=andthings-20" width="300" height="250" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe> -->
  <div style="text-align:center;background:whitesmoke;border-top:solid 1px #EEE;border-bottom:solid 1px #DDD;padding-top:20px;">
    <center>
      <script src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US&adInstanceId=8e39b095-6775-42d4-95dc-692fb4fedd38"></script>
    </center>
  </div>

  <style media="screen">
  /*body {
    background:whitesmoke;
  }
    #post {
      background:white;
      padding:25px 50px;
      box-shadow:0px 0px 10px #CCC;
      width:750px;
      margin:75px auto 50px;
    }*/
    #post {
      /*margin:50px 300px;*/
      max-width:750px;
      margin:50px auto;
    }
    pre {
      margin:0px !important;
    }
    code {
      /*padding-bottom:25px !important;*/
      font-size:14.5px;
    }
  </style>


    <div class="footer" style="padding:25px;font-size:12px;color:gray;">
      <small>      © 2017 Sean Behan</small>
      <a href="/sitemap.xml">Sitemap.xml</a>
    </div>
    </div>

    <script>hljs.initHighlightingOnLoad();</script>

    
      <script type="text/javascript">
        var _gauges = _gauges || [];
        (function() {
          var t   = document.createElement('script');
          t.type  = 'text/javascript';
          t.async = true;
          t.id    = 'gauges-tracker';
          t.setAttribute('data-site-id', '50b52f08f5a1f53f0e000032');
          t.setAttribute('data-track-path', 'https://track.gaug.es/track.gif');
          t.src = 'https://d36ee2fcip1434.cloudfront.net/track.js';
          var s = document.getElementsByTagName('script')[0];
          s.parentNode.insertBefore(t, s);
        })();
      </script>

      <script>
        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

        ga('create', 'UA-32797327-1', 'auto');
        ga('send', 'pageview');

      </script>
    
  </body>
</html>