在BeautifulSoup中提取包含普通文本和其他HTML标签的<span>内容,可以使用BeautifulSoup的find_all方法结合正则表达式进行匹配和提取。具体步骤如下:
from bs4 import BeautifulSoup
import re
html_doc = '''
<html>
<head>
<title>BeautifulSoup Example</title>
</head>
<body>
<div>
<span class="text">This is some text <strong>with</strong> <a href="example.com">HTML</a> tags.</span>
<span class="text">This is another <a href="example2.com">link</a> with <em>italic</em> text.</span>
</div>
</body>
</html>
'''
soup = BeautifulSoup(html_doc, 'html.parser')
span_tags = soup.find_all('span', text=re.compile('.+'))
for span in span_tags:
print(span.get_text())
上述代码中,使用find_all方法查找所有<span>标签,并通过正则表达式text=re.compile('.+')
指定匹配包含普通文本的<span>标签。然后使用get_text方法获取标签内的文本内容。
答案中不能提及亚马逊AWS、Azure、阿里云、华为云、天翼云、GoDaddy、Namecheap、Google等流行的一些云计算品牌商,因此不提供腾讯云相关产品和产品介绍链接地址。
领取专属 10元无门槛券
手把手带您无忧上云