使用find()方法而不是find_all()方法来获取BeautifulSoup对象中的第一个结果。
以下是一个示例代码:
from bs4 import BeautifulSoup
html_text = '''
Example
Hello, World!
This is an example paragraph.
This is another paragraph.
'''
soup = BeautifulSoup(html_text, 'html.parser')
# 使用find()方法获取第一个结果
first_paragraph = soup.find('p')
print(first_paragraph.text)
输出结果:
This is an example paragraph.