我有一个 HTML 如下:
<table class="stocksTable" summary="株価詳細">
<tr>
<th class="symbol"><h1>(株)みずほフィナンシャルグループ</h1></th>
<td class="stoksPrice realTimChange">
<div class="realTimChangeMod">
</div>
</td>
td class="stoksPrice">191.1</td>
<td class="change"><span class="yjSt">前日比</span><span class="icoUpGreen yjMSt">+2.5(+1.33%)</span></td>
</tr>
</table>
我试图提取 191.1
来自包含 td class="stoksPrice">191.1</td>
的行.
soup = BeautifulSoup(html)
res = soup.find_all('stoksPrice')
print (res)
但结果是[]
.
伙计们,如何找到它?
最佳答案
似乎有两个问题:
首先是您对 find_all
的使用是无效的。您搜索名为 stoksPrice
的标记名的当前方式这是错误的广告,您的标签是 table
, tr
, td
, div
, span
.您需要将其更改为:
>>> res = soup.find_all(class_='stoksPrice')
搜索具有该类的标签。
其次,您的 HTML 格式不正确。带有 stoksPrice
的列表是:
</td>
td class="stoksPrice">191.1</td>
应该是:
</td>
<td class)="stoksPrice">191.1</td>
(注意 <
在 td
之前)
不确定这是否是 Stack Overflow 中的复制错误,或者 HTML 最初格式不正确,但解析起来并不容易......
关于python - 无法从汤中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49663551/