Created
January 12, 2009 11:59
-
-
Save june29/45972 to your computer and use it in GitHub Desktop.
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
| require "rubygems" | |
| require "nokogiri" | |
| require "open-uri" | |
| require "uri" | |
| class SimplePager | |
| include Enumerable | |
| attr_reader :doc, :original_url, :current_url | |
| def initialize(original_url, next_link_xpath) | |
| @original_url = original_url | |
| @current_url = original_url | |
| @next_link_xpath = next_link_xpath | |
| @continue = true | |
| @doc = Nokogiri::HTML(open(original_url)) | |
| end | |
| def stop | |
| @continue = false | |
| end | |
| def next | |
| next_link = @doc.xpath(@next_link_xpath).first["href"] | |
| next_link = URI.split(@original_url)[0] + "://" + URI.split(@original_url)[2] + next_link unless /^http/ =~ next_link | |
| @doc = Nokogiri::HTML(open(next_link)) | |
| @current_url = next_link | |
| self | |
| end | |
| def each | |
| yield @doc | |
| if @continue | |
| self.next.each do |doc| | |
| yield doc | |
| end | |
| end | |
| end | |
| end |
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
| pager = SimplePager.new("http://june29.jp/", "//a[@rel='next']") | |
| pager.each do |doc| | |
| puts "------------------------" | |
| puts pager.current_url | |
| puts "------------------------" | |
| doc.xpath("//div[contains(concat(' ', normalize-space(@class), ' '), ' hentry ')]").each do |hentry| | |
| title = hentry.xpath("./h2[@class='entry-title']").text | |
| link = hentry.xpath("./h2[@class='entry-title']/a").attr("href") | |
| if /2008\/10/ =~ link | |
| pager.stop | |
| break | |
| end | |
| puts "#{title}\n\t#{link}" | |
| end | |
| sleep 5 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment