2013-04-28 141 views
0

我想解析下面的變量doc中顯示的URL。我的問題是與job變量。當我返回時,它會返回頁面上的所有職位,而不是給定評論的特定職位。有沒有人有建議如何返回我指的具體職位?如何使用Nokogiri解析頁面?

require 'nokogiri' 
require 'open-uri' 

# Perform a google search 
doc = Nokogiri::HTML(open('http://www.glassdoor.com/Reviews/Microsoft-Reviews-E1651.htm')) 

reviews = [] 


current_review = Hash.new 

doc.css('.employerReview').each do |item| 
    pro = item.parent.css('p:nth-child(1) .notranslate').text 
    con = item.parent.css('p:nth-child(2) .notranslate').text 
    job = item.parent.css('.review-microdata-heading .i-occ').text 
    puts job 
    advice = item.parent.css('p:nth-child(3) .notranslate').text 

    current_review = {'pro' => pro, 'con' => con, 'advice' => advice} 

    reviews << current_review 
end 

回答

0

貌似item.parent是在每種情況下#MainCol,換句話說整列。

item.parent.css更改爲item.css應解決您的問題。