【问题标题】:Css selector for an attribute whose value has whitespaces值包含空格的属性的 Css 选择器
【发布时间】:2016-10-23 00:48:39
【问题描述】:

我正在使用 library_Beautiful Soup 在 python 中编写代码,我想选择这个标签(使用 CSS 选择器):

<div>
...
  <div>
  ...
    <div style=" height:80px; width:700px; ">
     ...

我正在尝试:

soup.select('div div div[style*="height:80px; width:700px"]')

(CSS 选择器在单引号内,即

 'div div div[style*="height:80px; width:700px"]')

但这并没有选择任何标签。

我也试过了:

 soup.select('div div div[style*="height:80px;"][style*="width:700px"]')

但这也行不通。

我不允许这样做:

 soup.select('div div div[style=" height:80px; width:700px "]')

因为它引发了一个错误,指出这是一个无效的 css 选择器(不允许在开始双引号之后或在结束双引号之前使用空格)。

谁能给我推荐这个标签的正确 CSS 选择器?

【问题讨论】:

  • 你能添加一个codepen或其他东西来演示你的代码吗?

标签: python css beautifulsoup


【解决方案1】:

你可以使用find:

.find("div", style=" height:80px; width:700px; "))

在选择中使用多个属性不适用于 bs4,您也仅限于在按属性搜索时可以传递的内容,最好的办法是选择一个 .select_one("div[style*=height:80px;]"[style*=width:700px;]"

【讨论】:

    猜你喜欢
    • 2013-06-19
    • 2014-10-29
    • 2016-09-18
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    相关资源
    最近更新 更多