python - Extract integer from HTML file with BS4
问题描述
I am trying to extract the integer (0) from within the div of the class 'high' in beautifulsoup to store in a variable:
[<tr class="high">
<td>
<div>
<a href="#*_high">High</a>
</div>
</td>
<td style="text-align: center;">
<div>0</div>
</td>
</tr>]
I am able to extract the section above from the html file using
high = soup.find_all(class_="high")
However any attempts to filter down to just the integer result in empty results:
div = soup.find("div", class_= "High")
print(div)
Any help would be greatly appreciated!
解决方案
首先找到<tr class='high'>
标签。从那里找到这两个<td>
元素。第二个包含您想要的数据。例如:
from bs4 import BeautifulSoup
html = """<tr class="high">
<td>
<div>
<a href="#*_high">High</a>
</div>
</td>
<td style="text-align: center;">
<div>0</div>
</td>
</tr>"""
soup = BeautifulSoup(html, "html.parser")
tr = soup.find('tr', class_="high")
data = int(tr.find_all('td')[1].get_text(strip=True))
print(data)
推荐阅读
- hibernate - 使用 JOIN 向 JPA 查询添加其他列
- java - 我可以在java中将元素添加到空字符串向量吗?
- r - 测试 R 中是否存在(通配符)文件
- mysql - 如何编写复杂的 SQL 查询?
- unix - 基于时间戳为 unix 日志文件获取不同的错误
- django - 如何在 Django admine 内联中更改名称“+ 添加另一个 Model_name”按钮?
- tensorflow - Google Colab 上的 Keras YoloV3,AttributeError: module 'keras.backend' has no attribute 'control_flow_ops
- android - 如何从 Android.mk 构建错误中获取 LOCAL_STATIC_ANDROID_LIBRARIES 名称?
- visual-studio-code - 无法在 WSL 上启动 VScode Cloud Run 插件
- android - “Null”类型不是“Widget”类型的子类型