python - 使用 BeautifulSoup 进行 SRE 匹配时无法访问元素

标签 python web-scraping beautifulsoup

我像这样抓取页面:

s1 =bs4DerivativePage.find_all('table',class_='not-clickable zebra’) 

输出:

[<table class="not-clickable zebra" data-price-format="{price}" data-quote-detail="0" data-stream-id="723288" data-stream-quote-option="Standard">
 <tbody><tr>
 <td><strong>Stop loss-niveau</strong></td>
 <td>141,80447</td>
 <td class="align-left"><strong>Type</strong></td>
 <td>Turbo's</td>
 </tr>
 <tr>
 <td><strong>Financieringsniveau</strong></td>
 <td>135,05188</td>

我需要从 Financieringsniveau 检索该值。 以下给出了匹配:

finNiveau=re.search('Financieringsniveau’,LineIns1)

但是我需要数值135,05188。如何做到这一点?

最佳答案

您可以使用.findNext()

例如:

from bs4 import BeautifulSoup

s = """<table class="not-clickable zebra" data-price-format="{price}" data-quote-detail="0" data-stream-id="723288" data-stream-quote-option="Standard">
 <tbody><tr>
 <td><strong>Stop loss-niveau</strong></td>
 <td>141,80447</td>
 <td class="align-left"><strong>Type</strong></td>
 <td>Turbo's</td>
 </tr>
 <tr>
 <td><strong>Financieringsniveau</strong></td>
 <td>135,05188</td></tr></tbody></table>"""

soup = BeautifulSoup(s, "html.parser")
print(soup.find(text="Financieringsniveau").findNext("td").text)  #Search using text and the use findNext

输出:

135,05188

关于python - 使用 BeautifulSoup 进行 SRE 匹配时无法访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53667486/

相关文章:

python - 将工作代码移动到单独的模块时出现 unicode utf8 错误

python - 如何在不解析内部div的情况下提取外部div内容

python - NumPy 使用索引列表选择每行的特定列索引

python - 在 [Python] 中查找所有 TR(来自 html <table>)

python:将 numpy 数组数据类型从 int64 转换为 int

python - 如何在 Python 中使用 BeautifulSoup 在文本字符串后查找表格?

python - matplotlib的 "barh"中height变量的单位是什么?

python - 从表中垂直读取抓取的数据,而不是水平读取 Python

python - 如何使用 BeautifulSoup 从父标签和子标签中获取文本以放入 DOCX 表中

python - Beautifulsoup 的输出