标签:name tag com div code XML htm 属性 nbsp
BeautifulSoup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:Tag
,NavigableString
,BeautifulSoup
,Comment
.
Tag:
soup = BeautifulSoup(‘#<title>The Dormouses story</title>‘,"lxml") tag = soup.title print(tag) >> <title>The Dormouses story</title>
Tag有两个重要的属性:name和attrs;
soup = BeautifulSoup(‘#<title>The Dormouses story</title>‘,"lxml") tag = soup.title print(tag.name) >> title
#利用name属性修改html文档
soup = BeautifulSoup(‘#<title>The Dormouses story</title>‘,"lxml")
tag = soup.title
tag.name = ‘aaa‘
print(tag)
>> <aaa>The Dormouses story</aaa>
标签:name tag com div code XML htm 属性 nbsp
原文地址:https://www.cnblogs.com/weimusan/p/9278822.html