使用css选择器来选择具有某个类但没有样式属性的所有div元素 [英] Use css selector to select all div elements with some class, but without style attribute

查看:825
本文介绍了使用css选择器来选择具有某个类但没有样式属性的所有div元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 wombat 撰写抓取工具。和不知何故我使用CSS选择器,而不是XPATH。我有很难选择这里 - 我不能实现使用css。



我有div元素,我想从一个页面抓取:

 < div class =do_cat_ads_box> ...< / div> 
< div class =do_cat_ads_box2> ...< / div>
< div class =do_cat_ads_boxstyle =..> ...< / div>
< div class =do_cat_ads_box2style =..> ...< / div>

但是具有'style'属性的元素 - 是我不需要的垃圾/ p>

所以我的问题是,我可以用类'do_cat_ads_box'和'do_cat_ads_box2'获取所有的div元素,但是避免div元素有'style'属性?

我最终得到了类似这样的东西,它不工作:

  css = div.do_cat_ads_box2,div.do_cat_ads_box,div.do_cat_ads_box:not(@style)',:iterator do 
href'css = div.do_cat_ads_image a @href'
name'css = div.do_cat_ads_detail a'
end

如果它不是css选择器的两倍, 。但我对css选择器方法很感兴趣。

解决方案

CSS中的属性选择器使用 [attr] 符号。 @attr 符号属于属性定位器(以及XPath)。



假设Wombat支持CSS语法属性选择器,尝试将:not(@style)更改为:not([style])如下:

  application'css = div.do_cat_ads_box:not([style]),div.do_cat_ads_box2:not([ style])',:iterator do 
href'css = div.do_cat_ads_image a @href'
name'css = div.do_cat_ads_detail a'
end


I'm writing a crawler with wombat. And somehow i'm using CSS selectors, not XPATH. And i have very difficult selection here - that i can't achieve using css.

I have div elements that i want to grab from a page:

<div class="do_cat_ads_box"> ... </div>
<div class="do_cat_ads_box2"> ... </div>
<div class="do_cat_ads_box" style=".."> ...</div>
<div class="do_cat_ads_box2" style=".."> ... </div>

But elements with 'style' attribute - are garbage (ads) that i don't need.

So my question is, can I grab all div elements with classes 'do_cat_ads_box' and 'do_cat_ads_box2', but avoid div elements that have 'style' attribute?

I ended up with something like this and it is not working:

application 'css=div.do_cat_ads_box2, div.do_cat_ads_box,  div.do_cat_ads_box:not(@style)', :iterator do
  href 'css=div.do_cat_ads_image  a @href'
  name 'css=div.do_cat_ads_detail a'
end

if it's not double with css selectors, then there is always xpath way. But i'm very interested in css-selectors approach.

解决方案

Attribute selectors in CSS use [attr] notation. The @attr notation pertains to attribute locators (as well as XPath).

Assuming Wombat supports the CSS syntax for attribute selectors, try changing :not(@style) to :not([style]) and rewriting your class selectors to the following:

application 'css=div.do_cat_ads_box:not([style]), div.do_cat_ads_box2:not([style])', :iterator do
  href 'css=div.do_cat_ads_image  a @href'
  name 'css=div.do_cat_ads_detail a'
end

这篇关于使用css选择器来选择具有某个类但没有样式属性的所有div元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆