小问题 [我对 Python 的 BeautifulSoup()
不是很熟悉] 如果我有以下元素,
如何提取/获取“1 条评论”(或“2 条评论”等)?该“a
”标记中没有class
(或id
或其他属性)。
<td class="subtext">
<a href="item?id=22823679">1 comment</a>
</td>
最佳答案
下面怎么样,用本地html
文件测试
from bs4 import BeautifulSoup
url = "D:\\Temp\\example.html"
with open(url, "r") as page:
contents = page.read()
soup = BeautifulSoup(contents, 'html.parser')
element = soup.select('td.subtext')
value = element[0].get_text()
print(value)
example.html
<html>
<head></head>
<body>
<td class="subtext">
<a href="item?id=22823679">1 comment</a>
</td>
</body>
</html>
关于Python `beautifulsoup` 提取缺少 `class` 的 url,其他属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61132603/