BeautifulSoup中怎么获取字符串内容
在BeautifulSoup中,要获取标签内的字符串内容可以使用.string
属性或.get_text()
方法来实现。例如:
from bs4 import BeautifulSouphtml = """<html><body><p>这是一个段落</p></body></html>"""soup = BeautifulSoup(html, 'html.parser')p_tag = soup.find('p')# 使用.string属性获取字符串内容content = p_tag.stringprint(content)# 使用get_text()方法获取字符串内容content = p_tag.get_text()print(content)
以上代码会输出:
这是一个段落这是一个段落