要使用BeautifulSoup来查找特定类的span标签,并返回其内容,可以按照以下步骤进行操作:
from bs4 import BeautifulSoup
html = """
This is a span with class
This is a span without class
"""
soup = BeautifulSoup(html, 'html.parser')
spans = soup.find_all('span', {'class': 'my_class'})
if spans:
for span in spans:
print(span.text)
else:
print("")
完整的代码示例:
from bs4 import BeautifulSoup
html = """
This is a span with class
This is a span without class
"""
soup = BeautifulSoup(html, 'html.parser')
spans = soup.find_all('span', {'class': 'my_class'})
if spans:
for span in spans:
print(span.text)
else:
print("")
输出:
This is a span with class