[英]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
元素中的.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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.