在BeautifulSoup中,find()方法用于查找符合给定标准的第一个标签,并返回该标签。.find(text=True)是在查找标签的同时获取其文本内容。text=True是find()方法的一个关键字参数,用于返回标签中的文本内容。如果不设置text=True,它会将标签及其子标签一并返回。
代码示例:
from bs4 import BeautifulSoup
html = 'Hello, World!
'
soup = BeautifulSoup(html, 'html.parser')
# 查找文本内容为Hello的标签
result = soup.find(text='Hello')
print(result) # Hello
# 查找指定的标签,并返回其文本内容
result = soup.find('p').text
print(result) # Hello, World!