Nokogiri Exclude HTML Class



我正在尝试抓取所有在我们的Facebook群组中评论帖子的人的名字。我在本地下载了该文件,并能够抓取评论者的姓名以及回复这些评论的人的姓名。我只想要原始评论,而不是回复...似乎我必须排除 UFIReplyList 类,但我的代码仍在提取所有名称。任何帮助将不胜感激。谢谢!

require 'nokogiri'
require 'pry'
class Scraper
@@all = []
def get_page
file = File.read('/Users/mark/Desktop/raffle.html')
doc = Nokogiri::HTML(file)
# binding.pry
doc.css(".UFICommentContent").each do |post|
# binding.pry
author = post.css(".UFICommentActorName").css(":not(.UFIReplyList)").text
@@all << author
end
puts @@all
end
end
Scraper.new.get_page

遍历每个.UFICommentActorName元素的祖先,以拒绝包含在.UFIReplyList元素中的祖先。

@authors_nodes = doc.css(".UFICommentActorName").reject do |node|
# extract all ancestor class names; 
# beware of random whitespace and multiple classes per node
class_names = node.ancestors.map{ |a| a.attributes['class'].value rescue nil }
class_names = class_names.compact.map{ |names| names.split(' ') }
class_names = class_names.flatten.map(&:strip)
# reject if .UFIReplyList found
class_names.include?('UFIReplyList')
end
@authors_nodes.map(&:text)

最新更新