【问题标题】:Want to select a css path, but same selector occurs multiple times想选择一个css路径,但是同一个选择器出现多次
【发布时间】:2022-07-04 09:00:07
【问题描述】:

来自:https://www.funda.nl/en/koop/veenendaal/huis-42657821-roggeveld-59/ 我想通过 CSS 选择器选择居住区和地块大小。

因此,我找到了以下css路径:

response.css('span.kenmerken-highlighted__value.fd-text--nowrap').xpath('normalize 
space()')

当我这样做时,我得到以下输出:

[<Selector xpath='normalize-space()' data='123 m²'>, <Selector xpath='normalize-space()' data='187 m²'>, <Selector xpath='normalize-space()' data='4'>]

这是因为网站上的多个值具有相同的 css 路径。如果我想指定例如 187 m2 或“4”,我该怎么办?

【问题讨论】:

  • 已经知道了,谢谢

标签: python css scrapy


【解决方案1】:

试试下面的 xpath 选择器

>>> response.xpath("//*[@title='plot size']/following-sibling::span[1]/text()").get()
'187 m²'
>>> response.xpath("//*[@title='bedrooms']/following-sibling::span[1]/text()").get()
'4'

对于 css 选择器,您可以使用以下

>>> response.css("[title='plot size'] + span ::text").get()
'187 m²'
>>> response.css("[title='bedrooms'] + span ::text").get()
'4'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    相关资源
    最近更新 更多