python - 无法从汤中获取文本

标签 python html beautifulsoup

我有一个 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/

相关文章:

python - 使用 BeautifulSoup 将 <a> 定位到特定属性

html - react 中的 <details> 是什么?

javascript - 如何在加载事件中将数字转换为单词?

python - 使用 Python 抓取 html 内容中的内容

python初学者: try two things before exception?

jquery - 获取类名中的数字 - jquery

python - BeautifulSoup 返回关闭标签而不是标签文本

Python while 循环在快速排序期间卡住

python - Azure 日志分析 : How to send custom python pandas DataFrames into LAW

Python列表实现和pympler测量