我很困惑。我有一块HTML,我从一张大桌子上刮掉了。它看起来大约是这样的:为什么这个listiterator卡住了?
<td align="left" class="page">Number:\xc2\xa0<a class="topmenu" href="http://www.example.com/whatever.asp?search=724461">724461</a> Date:\xc2\xa01/1/1999 Amount:\xc2\xa0$2.50 <br/>Person:<br/><a class="topmenu" href="http://www.example.com/whatever.asp?search=LAST&searchfn=FIRST">LAST,\xc2\xa0FIRST </a> </td>
(事实上,它看起来更糟,但我regexed了大量换行符)
我需要这些行,并打破了日期/金额线。它似乎是要开始找到HTML块的孩子的地方。该块是一个字符串,因为这正是正则表达式给我的回报。所以我做:
text_soup = BeautifulSoup(text)
text_children = text_soup.find('td').childGenerator()
我可以通过孩子与迭代
for i,each in enumerate(text_soup.find('td').childGenerator()):
print type(each)
print i, ":", each
但与
for i, each in enumerate(text_children):
...etc
这些应该是相同的。所以我很困惑。
你的意思是我为每个枚举(text_children):'? –
他们应该工作一样。记住你只能迭代text_children一次,因为它会消耗生成器。 –
@gnibbler,谢谢你指出我跳过了“枚举” - 我在控制台中做得很好。我要澄清我的问题,但是......你是什么意思,我只能迭代text_children一次? – Amanda