title: How to write a while loop properly
body: <p>I'm trying to scrape a website however I cannot seem to get my while-loop to break out once it hits a page with no more information:</p>
  
  <pre><code>def scrape_verse_items(keyword)
  
    pg = 1
  
    while pg &lt; 1000
      puts "page #{pg}"
      url = "https://www.bible.com/search/bible?page=#{pg}&amp;q=#{keyword}&amp;version_id=1"
  
      doc = Nokogiri::HTML(open(url))
      items = doc.css("ul.search-result li.reference")
      error = doc.css('div#noresults')
  
      until error.any? do
        if keyword != '' 
          item_hash = {}
          items.each do |item|
            title = item.css("h3").text.strip
            content = item.css("p").text.strip
            item_hash[title] = content
  
          end
        else
          puts "Please enter a valid search"
        end 
  
        if error.any? 
          break
        end
      end
      pg += 1
    end
    item_hash
  end
  
  puts scrape_verse_items('joy')
  </code></pre>