Skip to main content
 首页 » 编程设计

python之排除要抓取的元素

2024年10月17日7rubylouvre

我试图从列表中排除某些元素。

在页面上http://www.persimmonhomes.com/rooley-park-10126有一些我想废弃的元素是 (div class="housetype js-filter-housetype") 有一些我不想废弃的元素是 (div class="housetype js-filter-housetype"style= “显示:无;”)

html 看起来像这样:

<div class="housetype js-filter-housetype">  
<div class="housetype js-filter-housetype">  
<div class="housetype js-filter-housetype">  
<div class="housetype js-filter-housetype"> 
<div class="housetype js-filter-housetype">  
<div class="housetype js-filter-housetype" style="display: none;"> 
<div class="housetype js-filter-housetype" style="display: none;"> 

我正在尝试编写代码来排除 div class="housetype js-filter-housetype"style="display: none;"。

我目前的代码是:

start_urls = [ 
    "http://www.persimmonhomes.com/rooley-park-10126", 
] 
 
def parse(self, response): 
    for sel in response.xpath('//*[@id="aspnetForm"]/div[4]'): 
        item = PersimmonItem() 
        item['housetypeheading'] = sel.xpath('//*[@class="houses-list js-scrollable js-filterable js-houselist"]//*[not(@style="display: none;")]/h2[@class="housetype__heading"]').extract() 
        yield item 

到目前为止,这不起作用。它只是废弃所有元素,无论它是否具有该部分 (style="display: none;")。我也尝试过 [not(contains(@style, "display: none;"))] - 但到目前为止没有运气。

我可以问点什么吗?

请您参考如下方法:

如果你想忽略所有带有样式属性的:

"//div[@class='housetype js-filter-housetype' and not(@style)]" 

或者那种特定的风格,只需使用和:

"//div[@class='housetype js-filter-housetype' and not(contains(@style,'display: none;'))]"