码迷,mamicode.com
首页 > 其他好文 > 详细

BeautifulSoup

时间:2018-07-07 23:58:15      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:name   tag   com   div   code   XML   htm   属性   nbsp   

BeautifulSoup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种: TagNavigableStringBeautifulSoupComment.
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>

 





BeautifulSoup

标签:name   tag   com   div   code   XML   htm   属性   nbsp   

原文地址:https://www.cnblogs.com/weimusan/p/9278822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!