这二者的区别是一个列表,一个是迭代器
it=soup.body.contents
type(it)
Out[116]: list
所以对应children对象而言,可以使用next()函数
it=soup.body.children
next(it)
Out[106]: '\n'
next(it)
Out[107]: <p class="title"><b>The Dormouse's story</b></p>
next(it)
Out[108]: '\n'
next(it)
Out[109]:
<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
next(it)
Out[110]: '\n'
next(it)
Out[111]: <p class="story">...</p>
next(it)
Out[112]: '\n'