【发布时间】:2022-01-27 04:46:46
【问题描述】:
我正在尝试使用 Python 和 BeautifulSoup 从 html 文件中获取 div 元素内的文本 Something here I want to get。
这就是部分代码在 html 中的样子:
<div xmlns="" id="idp46819314579224" style="box-sizing: border-box; width: 100%; margin: 0 0 10px 0; padding: 5px 10px; background: #d43f3a; font-weight: bold; font-size: 14px; line-height: 20px; color: #fff;" class="" onclick="toggleSection('idp46819314579224-container');" onmouseover="this.style.cursor='pointer'">Something here I want to get<div id="idp46819314579224-toggletext" style="float: right; text-align: center; width: 8px;">
-
</div>
</div>
这就是我尝试做的:
vu = soup.find_all("div", {"style" : "background: #d43f3a"})
for div in vu:
print(div.text)
我使用循环是因为有几个具有不同 id 的 div,但它们都具有相同的背景颜色。它没有错误,但我没有输出。
如何以背景色为条件获取文本?
【问题讨论】:
-
将其更改为
background-color: #d43f3a有帮助吗? -
可以运行,但还是没有输出。是因为不能用style作为条件吗?
标签: python parsing beautifulsoup