You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python提取HTML指定Span类的纯文本内容?

解决BeautifulSoup提取span纯文本的问题

你已经定位到目标span元素,要获取其纯文本内容,只需调用元素的.text属性(或.get_text()方法)即可,修改后的代码如下:

处理多个匹配元素

find_all返回的是元素列表,需要遍历列表获取每个元素的纯文本:

import requests
from bs4 import BeautifulSoup

res = requests.get('https://coinmarketcap.com/currencies/chainlink/')
soup = BeautifulSoup(res.text, 'lxml')

values = soup.find_all("span", class_="sc-f70bb44c-0 jxpCgO base-text")
# 遍历输出每个元素的纯文本
for value in values:
    print(value.text)

处理单个匹配元素

如果只需要第一个匹配的span内容,用find代替find_all更高效:

import requests
from bs4 import BeautifulSoup

res = requests.get('https://coinmarketcap.com/currencies/chainlink/')
soup = BeautifulSoup(res.text, 'lxml')

value = soup.find("span", class_="sc-f70bb44c-0 jxpCgO base-text")
# 判空后输出文本
if value:
    print(value.text)

执行后就能得到你想要的$14.22这类纯文本内容。

内容的提问来源于stack exchange,提问作者Bùi Huy Trường

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 20:33:24