Class: WebPageParser::GuardianPageParserV2

Inherits:
BaseParser
  • Object
show all
Defined in:
lib/web-page-parser/parsers/guardian_page_parser.rb

Overview

GuardianPageParserV2 parses Guardian web pages using html parsing. It can parse articles old and new but sometimes has slightly different results due to it stripping most html tags (like <strong>) which the V1 parser didn’t do.

Instance Attribute Summary

Attributes inherited from BaseParser

#url

Instance Method Summary collapse

Methods inherited from BaseParser

#guid, #guid_from_url, #hash, #initialize, #page, #retrieve_page

Constructor Details

This class inherits a constructor from WebPageParser::BaseParser

Instance Method Details

#contentObject



63
64
65
66
67
68
69
# File 'lib/web-page-parser/parsers/guardian_page_parser.rb', line 63

def content
  return @content if @content
  story_body = html_doc.css('div#article-body-blocks *').select do |e|
    e.name == 'p' or e.name == 'h2' or e.name == 'h3'
  end
  story_body.collect { |p| p.text.empty? ? nil : p.text.strip }.compact
end

#dateObject



71
72
73
74
75
76
77
# File 'lib/web-page-parser/parsers/guardian_page_parser.rb', line 71

def date
  return @date if @date
  if date_meta = html_doc.at_css('meta[property="article:published_time"]')
    @date = DateTime.parse(date_meta['content']) rescue nil
  end
  @date
end

#filter_url(url) ⇒ Object



79
80
81
82
# File 'lib/web-page-parser/parsers/guardian_page_parser.rb', line 79

def filter_url(url)
  # some wierd guardian problem with some older articles
  url.to_s.gsub("www.guprod.gnl", "www.guardian.co.uk") 
end

#html_docObject



55
56
57
# File 'lib/web-page-parser/parsers/guardian_page_parser.rb', line 55

def html_doc
  @html_document ||= Nokogiri::HTML(page)
end

#titleObject



59
60
61
# File 'lib/web-page-parser/parsers/guardian_page_parser.rb', line 59

def title
  @title ||= html_doc.css('div#main-article-info h1:first').text.strip
end