.contents 和 .children
head_tag = soup.head
head_tag
# <head><title>The Dormouse's story</title></head>
head_tag.contents
[<title>The Dormouse's story</title>]
title_tag = head_tag.contents[0]
title_tag
# <title>The Dormouse's story</title>
title_tag.contents
# [u'The Dormouse's story']len(soup.contents)
# 1
soup.contents[0].name
# u'html'text = title_tag.contents[0]
text.contents
# AttributeError: 'NavigableString' object has no attribute 'contents'Last updated