要使用BeautifulSoup来检查span类和rel,你可以按照以下步骤操作:
pip install beautifulsoup4
from bs4 import BeautifulSoup
html_doc = """
Example Page
This is an example span
"""
soup = BeautifulSoup(html_doc, 'html.parser')
span_tags = soup.find_all('span', class_='example', rel='nofollow')
可以通过指定class_参数来匹配class属性,通过指定其他属性的名称和值来匹配其他属性。
for span in span_tags:
print(span.text)
这将打印出所有满足条件的span标签的内容。
完整的示例代码如下:
from bs4 import BeautifulSoup
html_doc = """
Example Page
This is an example span
"""
soup = BeautifulSoup(html_doc, 'html.parser')
span_tags = soup.find_all('span', class_='example', rel='nofollow')
for span in span_tags:
print(span.text)
这将输出:
This is an example span
希望这可以帮助到你!