Skip to content

Instantly share code, notes, and snippets.

@fxxkscript
Created May 1, 2013 15:25
Show Gist options
  • Select an option

  • Save fxxkscript/5495961 to your computer and use it in GitHub Desktop.

Select an option

Save fxxkscript/5495961 to your computer and use it in GitHub Desktop.
joke.rb
#!/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