为什么beautifulsoup的children不能用列表索引index去返回值 ?

首先html代码段:
<div class="element"><a href="/photoid/405270" target="_blank"><img src="http://image.xitek.com/photois ... ot%3B style ="width:763px;height:300px" alt="秀摄可餐作品:巍巍太行"></a><div class="info"><div class="title"><span class="award">[200906入围]</span>巍巍太行</div><div class="views">10504</div></div><div class="info"><div class="author"><a href="/uid/282056" target="_blank">秀摄可餐</a></div><div class="good">91</div><div class="replys">23</div></div></div><div class="element"><a href="/photoid/415269" target="_blank"><img src="http://image.xitek.com/photois ... ot%3B style ="width:449px;height:300px" alt="一二丁作品:“海底世界”"></a><div class="info"><div class="title"><span class="award">[200907二等]</span>“海底世界”</div><div class="views">17206</div></div><div class="info"><div class="author"><a href="/uid/697610" target="_blank">一二丁</a></div><div class="good">257</div><div class="replys">72</div></div></div><div class="element"><a href="/photoid/322391" target="_blank"><img src="http://image.xitek.com/photois ... ot%3B style ="width:400px;height:300px" alt="老白兔作品:百合花"></a><div class="info"><div class="title"><span class="award">[200807二等]</span>百合花</div><div class="views">74744</div></div><div class="info"><div class="author"><a href="/uid/538551" target="_blank">老白兔</a></div><div class="good">282</div><div class="replys">104</div></div></div><div class="element"><a href="/photoid/513526" target="_blank"><img src="http://image.xitek.com/photois ... ot%3B style ="width:480px;height:300px" alt="志海作品:飞翔中一瞬间"></a><div class="info"><div class="title"><span class="award">[201010二等]</span>飞翔中一瞬间</div><div class="views">15182</div></div><div class="info"><div class="author"><a href="/uid/1072286" target="_blank">志海</a></div><div class="good">181</div><div class="replys">45</div></div></div><div class="element"><a href="/photoid/476899" target="_blank"><img src="http://image.xitek.com/photois ... ot%3B style ="width:449px;height:300px" alt="摄谜谜作品:云中睡美人"></a><div class="info"><div class="title"><span class="award">[201005入围]</span>云中睡美人</div><div class="views">19891</div></div><div class="info"><div class="author"><a href="/uid/1046354" target="_blank">摄谜谜</a></div><div class="good">780</div><div class="replys">189</div></div></div><div class="element"><a href="/photoid/660026" target="_blank"><img src="http://image.xitek.com/photois ... ot%3B style ="width:450px;height:300px" alt="橄榄树作品:清迈大佛塔寺"></a><div class="info"><div class="title"><span class="award">[201311入围]</span>清迈大佛塔寺</div>
 
        element_link=bs.find_all('div',class_="element")
print len(element_link)
for href in element_link:

print type(href)
print href.tag

if href.children[0]:
print href.children[0]


获取得到的children是一个list,可是不能够使用序号 children[0]来获取某个子标签,为什么 ?
已邀请:

李魔佛 - 公众号:可转债量化分析 【论坛注册:公众号后台留言邮箱】

赞同来自:

从报错的 代码来看:
 
  File /main.py", line 77, in __download
print href.children[0]
TypeError: 'listiterator' object has no attribute '__getitem__'

children返回的是一个list迭代器,只能使用 for i in href.children: 来循环获得。

要回复问题请先登录注册