Class: Amazoned::Parser
- Inherits:
-
Object
- Object
- Amazoned::Parser
- Defined in:
- lib/amazoned/parser.rb
Instance Attribute Summary collapse
-
#html_doc ⇒ Object
readonly
Returns the value of attribute html_doc.
-
#product_hash ⇒ Object
Returns the value of attribute product_hash.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #call ⇒ Object
- #extract_subcategory_rankings(nokogiri_html) ⇒ Object
-
#initialize(response) ⇒ Parser
constructor
A new instance of Parser.
- #parse_response_for_product_details(response) ⇒ Object
Constructor Details
#initialize(response) ⇒ Parser
Returns a new instance of Parser.
5 6 7 8 9 |
# File 'lib/amazoned/parser.rb', line 5 def initialize(response) @product_hash = Hash.new @response = response @html_doc = Nokogiri::HTML(response.body) end |
Instance Attribute Details
#html_doc ⇒ Object (readonly)
Returns the value of attribute html_doc.
3 4 5 |
# File 'lib/amazoned/parser.rb', line 3 def html_doc @html_doc end |
#product_hash ⇒ Object
Returns the value of attribute product_hash.
2 3 4 |
# File 'lib/amazoned/parser.rb', line 2 def product_hash @product_hash end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
3 4 5 |
# File 'lib/amazoned/parser.rb', line 3 def response @response end |
Instance Method Details
#call ⇒ Object
11 12 13 |
# File 'lib/amazoned/parser.rb', line 11 def call parse_response_for_product_details( response ) end |
#extract_subcategory_rankings(nokogiri_html) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/amazoned/parser.rb', line 90 def extract_subcategory_rankings(nokogiri_html) # Below is gnarly string manipulation to parse text strings like: # "\n #2\n in Baby > Baby Care > Health\n \n #2\n in Baby > Baby Care > Pacifiers, Teethers & Teething Relief > Teethers\n " # into: # [["2", "Baby > Baby Care > Health"], ["2", "Baby > Baby Care > Pacifiers, Teethers & Teething Relief > Teethers"]] nokogiri_html .map{|i| i.text} .map{|i| i.partition("in") .map(&:strip)} .map{|i| i - ["in"] } .map{|i| i.map{|ii| ii.gsub("#", "") # remove '#' from '#2' .gsub("\u00A0", "") # remove No-Break Space Unicode characters (U+00A0) since Ruby's .strip command won't remove them } }.each do |i| hsh = {} hsh[:rank] = i.first.to_i hsh[:ladder] = i.last product_hash[:best_sellers_rank] << hsh end end |
#parse_response_for_product_details(response) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/amazoned/parser.rb', line 15 def parse_response_for_product_details(response) product_hash[:best_sellers_rank] = [] ######## # # Parent category Seller Rank Parser ######## parsed_parent_category = html_doc.css('#SalesRank').text.partition("(").first.chop.partition("#").last.partition("in").map(&:strip) - ["in"] product_hash[:rank] = parsed_parent_category.first.delete(',').to_i # "903,610" -> 903610 product_hash[:category] = parsed_parent_category.last ######## # # Subcategory Seller Rank Parser ######## extract_subcategory_rankings( html_doc.css('.zg_hrsr_item') ) ######## # # Package Dimension Parser ######## # Package Dimension Parsing Strategy 1: product_hash[:package_dimensions] = html_doc.css('.size-weight').children.map{|r| r.text}.reject{|r| !r.match?("inches")}.first # Package Dimension Parsing Strategy 2: if product_hash[:package_dimensions].blank? # Find an index for the string "Package Dimensions" within a string text extraction of the page str_index = html_doc.inner_text.index("Package Dimensions") unless str_index.nil? # Reduce string representing the html page down to a smaller target string including "Package Dimensions" and the weights str = html_doc.inner_text[str_index .. str_index + 150] # Find within target string an index for where the word "inches" appears, then grab characters around it product_hash[:package_dimensions] = str[str.index("inches")- 20.. str.index("inches")+8].strip end end # Package Dimension Parsing Strategy 3: response.search('.//*[@class="a-color-secondary a-size-base prodDetSectionEntry"]').map{|n| n.parent}.each do |n| # Parse html in each row of Amazon's product details table to get back a string. E.g: "\n \n Best Sellers Rank\n \n \n \n \n #63 in Toys & Games (See Top 100 in Toys & Games)\n \n \n #3 in Toys & Games > Baby & Toddler Toys > Teethers\n \n \n \n \n " str = n.children.inner_text if product_hash[:best_sellers_rank].blank? str.match("Best Sellers Rank") do |m| # Gnarly string manipulation extracts the array: ["63", "in", "Toys & Games"] parsed_parent_category = str.partition("(").first.chop.partition("#").last.partition("in").map(&:strip) # From ["63", "in", "Toys & Games"] we only care about first & last parts of this array product_hash[:rank] = parsed_parent_category.first.delete(',').to_i product_hash[:category] = parsed_parent_category.last parsed_categories = str.partition(")").last.split("#").map(&:strip) parsed_categories.each do |pc| next if pc.blank? parsed_category = pc.partition("in").map(&:strip).map{|i| i.gsub("#", "")} - ["in"] hsh = {} hsh[:rank] = parsed_category.first.delete(',').to_i hsh[:ladder] = parsed_category.last product_hash[:best_sellers_rank] << hsh end end end if product_hash[:product_dimensions].blank? # Use pattern matching to extract the product details we care about str.match("Product Dimensions") do |m| product_hash[:package_dimensions] = str[str.index("inches")- 20.. str.index("inches")+8].strip end end end product_hash end |