首页主机资讯BeautifulSoup中怎么获取字符串内容

BeautifulSoup中怎么获取字符串内容

时间2024-06-04 02:02:03发布访客分类主机资讯浏览661
导读:在BeautifulSoup中,要获取标签内的字符串内容可以使用.string属性或.get_text( 方法来实现。例如: from bs4 import BeautifulSoup html = """ <html> &l...

在BeautifulSoup中,要获取标签内的字符串内容可以使用.string属性或.get_text()方法来实现。例如:

from bs4 import BeautifulSoup

html = """
html>
    
body>
    
p>
    这是一个段落/p>
    
/body>
    
/html>
    
"""

soup = BeautifulSoup(html, 'html.parser')
p_tag = soup.find('p')

# 使用.string属性获取字符串内容
content = p_tag.string
print(content)

# 使用get_text()方法获取字符串内容
content = p_tag.get_text()
print(content)

以上代码会输出:

这是一个段落
这是一个段落

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: BeautifulSoup中怎么获取字符串内容
本文地址: https://pptw.com/jishu/674634.html
少儿编程洗牌后拼什么 BeautifulSoup中的NavigableString对象是什么

游客 回复需填写必要信息