我正在努力搜寻所有在我们的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
答案 0 :(得分:0)
遍历每个.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)