BeautifuSoup实用方法属性总结
一、对象 Beautifulsoup中有4个重要的对象: Tag,标签,可以获取标签文本,属性 BeautifulSoup,继承自Tag,所以Tag的方法它基本都能用 NavigableString,文本字符串 Comment,注释 二、创建BeautifulSoup对象 2.1 通过字符串创建 from bs4 import BeautifulSoup html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="story">Once upon a time there were three little sisters; and their names were </p> """ soup = BeautifulSoup(html, 'html.parser') print(soup.prettify()) 2.2 通过文件创建 from bs4 import BeautifulSoup with open(r"F:\tmp\etree.html") as fp: soup = BeautifulSoup(fp,"lxml") print(soup.prettify()) 三、Tag关系属性 关系 说明 parent 父节点 parents 祖先节点 next