gpt4 book ai didi

html - 如何访问带样式的 div 内的跨度?

转载 作者:行者123 更新时间:2023-11-28 18:03:50 24 4
gpt4 key购买 nike

我有这样的 HTML:

<div style = 'display: hidden'>
<span class = "thing">text</span>
</div>

<div style = 'display: block'>
<span class = "thing">text</span>
</div>

<div style = 'display: hidden'>
<span class = "thing">text</span>
</div>

我只想选择 <span>出现在未隐藏的 div 中的带有类“thing”的标签。我如何使用 Nokogiri gem 做到这一点?

这就是我正在尝试的:

page = Nokogiri::HTML(open(@url))
item_list = page.css("div[@style != 'display: hidden'] span.thing")

最佳答案

这是我会做的:

require 'nokogiri'

doc = Nokogiri::HTML.parse <<-eotl
<div style = 'display: hidden'> <span class = "thing">text1</span> </div>
<div style = 'display: block'> <span class = "thing">text2</span> </div>
<div style = 'display: hidden'> <span class = "thing">text3</span> </div>
<div style = 'display: foo'> <span class = "thing">text4</span> </div>
eotl

doc.css("div:not([style$=hidden])>span.thing").size # => 2

doc.css("div:not([style$=hidden])>span.thing").each do |tag|
p [tag.name,tag.text]
end
# >> ["span", "text2"]
# >> ["span", "text4"]

CSS选择器中主要的两个,我想给大家介绍一下,如下:

:not

Selects all elements that do not match the given selector.

[attribute$=value]

Selects elements that have the specified attribute with a value ending exactly with a given string. The comparison is case sensitive.

关于html - 如何访问带样式的 div 内的跨度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19963564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com