Created
May 1, 2013 15:25
-
-
Save fxxkscript/5495961 to your computer and use it in GitHub Desktop.
joke.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'nokogiri' | |
| require 'open-uri' | |
| class Joke | |
| attr_reader :doc, :next, :res | |
| def initialize(url, n) | |
| @res = [] | |
| while n > 0 | |
| @doc = Nokogiri::HTML(open(url)) | |
| @next = url + @doc.css('.next')[0]['href'] | |
| @res += self.getJokes | |
| n -= 1 | |
| end | |
| end | |
| def getJokes | |
| arr = [] | |
| @doc.css('.col1 .block .content').each do |content| | |
| arr.push(content.content.strip) | |
| end | |
| arr | |
| end | |
| end | |
| first = Joke.new('http://www.qiushibaike.com', 30) | |
| first.res.each_with_index do |content, index| | |
| puts index, content, "\n" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment