我使用Nokogiri来解析html。对于所显示的网站,我试图创建一个哈希数组,其中每个哈希将包含优点,缺点和建议部分,为网站上显示的给定评论。我在做这件事时有麻烦,希望在这里得到一些建议。当我返回某个元素时,我无法在站点上显示正确的内容。什么好主意吗?
require 'open-uri'
require 'nokogiri'
# 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
advice = item.parent.css('p:nth-child(3) .notranslate').text
current_review = {'pro' => pro, 'con' => con, 'advice' => advice}
reviews << current_review
end
试试这个:
reviews = []
doc.css('.employerReview').each do |item|
pro, con, advice = item.css('.description .notranslate text()').map(&:to_s)
reviews << {'pro' => pro, 'con' => con, 'advice' => advice}
end
ruby也倾向于使用符号键,所以除非你需要它们是字符串,否则我会使用
reviews << { pro: pro, con: con, advice: advice }