class PinkoiScraper::Filter
filter class basically uses xpath selectors to get attribs
Attributes
item_selector[W]
price_selector[W]
result[R]
site_url[W]
title_selector[W]
Public Class Methods
new()
click to toggle source
# File lib/pinkoi/pinkoi_scraper.rb, line 26 def initialize @result = [] # xml selectors that will be used to scrape data @item_selector = "//div[contains(@class,\'items\')]/div" @title_selector = "div[contains(@class,\'title\')]" @price_selector = "div[@class=\'info\']/div[@class=\'price\']" @site_url = 'http://www.pinkoi.com/browse?' end
Public Instance Methods
fetch_result(uri = 'category=1')
click to toggle source
# File lib/pinkoi/pinkoi_scraper.rb, line 35 def fetch_result(uri = 'category=1') url = @site_url + uri # try to open the url document = get_xmldata(url) # hard return on an error return [] unless document != 'error' items = document.xpath(@item_selector) # loop through the items and get the title and price items.map do |item| title = item.xpath(@title_selector).text price = item.xpath(@price_selector).text @result << { title: "#{title}", price: "#{price}" } unless title.empty? end result end
Private Instance Methods
get_xmldata(url)
click to toggle source
# File lib/pinkoi/pinkoi_scraper.rb, line 17 def get_xmldata(url) raw_html = open(url) Oga.parse_html(raw_html) rescue StandardError 'error' end