开发者

Extracting links in a class

doc.xpath("//div[@id='Ci_']").each_with_index do |div,i|
parse_file.puts "#{div.at_xpath("./*[@class='class1']").text}"
parse_file.puts "#{div.at_xpath("./*[@class='class2']").text}"
开发者_如何转开发

There are two links in class2 and i need to extract text that belongs to these links, separately. How can I do that?


Assuming that the anchors have the class you want (and are not under a parent with that class):

doc = Nokogiri::HTML('<div><a class="c2">foo</a><a class="c2">bar</a></div>')
div = doc.at_css('div')
both= div.xpath('./*[@class="c2"]/text()').map(&:text)
p both
#=> ["foo", "bar"]

If the anchors are within the class:

doc = Nokogiri::HTML('<div><b class="c2"><a>foo</a><a>bar</a></b></div>')
div = doc.at_css('div')
both= div.xpath('./*[@class="c2"]//a/text()').map(&:text)
p both
#=> ["foo", "bar"]


Try this:

doc.search("#Ci_ .class1 a").first.text
doc.search("#Ci_ .class2 a").last.text
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜