SQLTerminal.app
now available

XPath.app
now available
Want fresh tech tips in your inbox?

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>

  


  <div class="" style="max-width:500px;margin:0px auto;padding-top:30px;">
    <h1 style="margin:0px;border-top:solid 1px #EEE;padding-top:15px;color:gray">Apps I've Built</h1>
    <style media="screen">
  body table td {
  }
</style>
<table id="my-apps" style="width:100%">
  <tr>
    <td style="padding:10px">
      <div class="icon" style=";width:100px;border-radius:20px;color:#00C8A0;">
        <a href="https://t.co/6S9Ln97pWB">
          <img src="/static/xpath.jpg" alt="" style='width:100px;border-radius:20px;'>
        </a>
      </div>
    </td>
    <td style="text-align:left;padding:10px">
      <a style="font-size:24px;color:#643FB0;" href="https://t.co/6S9Ln97pWB">XPath Expression Editor</a><br>
      Practice and improve your XPath skills with XPath Editor
      <br>
      <small><a href="https://t.co/6S9Ln97pWB" style="color:gray;text-decoration:underline" target="_blank">Click to buy on the App Store</a></small>
    </td>
  </tr>
  <tr>
    <td style="padding:10px">
      <div class="icon" style=";width:100px;border-radius:20px;color:#00C8A0;">
        <a href="https://bitly.com/2xUApBP">
          <img src="/static/photolocationchanger.png" alt="" style='width:100px;border-radius:20px;'>
        </a>
      </div>
    </td>
    <td style="text-align:left;padding:10px">
      <a style="font-size:24px;color:#0b8169;" href="https://itunes.apple.com/us/app/change-location-for-pics-vids/id1318265235?mt=8">Photo Location Changer</a><br>
      Easily change the location on your photos and videos
      <br>
      <small><a href="http://www.photodatechanger.com/photo_location_changer" style="color:gray;text-decoration:underline" target="_blank">Click here for more info about the app</a></small>
    </td>
  </tr>
  <tr>
    <td style="padding:10px">
      <div class="icon" style="background:rgb(208,85,67);width:100px;border-radius:20px;">
        <a href="https://bitly.com/2xUApBP">
          <img src="/static/photodatechanger.png" alt="" style='width:100px;'>
        </a>
      </div>
    </td>
    <td style="text-align:left;padding:10px">
      <a style="font-size:24px;" href="https://bitly.com/2xUApBP">Photo Date Changer</a><br>
      Easily change the dates and times on your photos and videos
      <br>
      <small><a href="http://www.photodatechanger.com/?from=www.seanbehan.com" style="color:gray;text-decoration:underline" target="_blank">Click here for more info about the app</a></small>
    </td>
  </tr>
  <tr>
    <td style="padding:10px">
      <div class="icon" style="background:#E56D68;width:100px;border-radius:20px;">
        <a href="http://apple.co/2lUJgwq">
          <img src="/static/vocabreminder.png" alt="" style='width:100px;'>
        </a>
      </div>
    </td>
    <td style="text-align:left;padding:10px">
      <a style="font-size:24px;" href="http://apple.co/2lUJgwq">VocabReminder</a><br>
      English dictionary with notifications so you won't forget what you're studying!
      <br>
      <small><a href="http://www.vocabreminder.com/?from=www.seanbehan.com" style="color:gray;text-decoration:underline" target="_blank">Click here for more info about the app</a></small>
    </td>
  </tr>
  <tr>
    <td style="padding:10px">
      <div class="icon" style=";width:100px;border-radius:20px;">
        <a href="https://itunes.apple.com/us/app/vocabquiz-quiz-yourself-on-vocabulary/id1252399004?mt=8">
          <img src="/static/vocabquiz.png" alt="" style='width:100px;border-radius:20px'>
        </a>
      </div>
    </td>
    <td style="text-align:left;padding:10px">
      <a style="font-size:24px;color:rgb(55, 80, 143)" href="https://itunes.apple.com/us/app/vocabquiz-quiz-yourself-on-vocabulary/id1252399004?mt=8">VocabQuiz</a><br>
      The app that quizzes and scores you on your vocabulary!
      <br>
      <small><a href="https://itunes.apple.com/us/app/vocabquiz-quiz-yourself-on-vocabulary/id1252399004?mt=8" style="color:gray;text-decoration:underline" target="_blank">Click here for more info about the app</a></small>
    </td>
  </tr>
</table>
    <hr>
  </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>

      </div>

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