我像这样抓取页面:
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/