【发布时间】:2012-11-21 03:46:32
【问题描述】:
我非常困惑。我有一块 HTML 是我从一个更大的表格中刮下来的。它看起来像这样:
<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>
(其实看起来更糟,但是我正则表达式掉了很多换行符)
我需要把这些线弄出来,把日期/金额线分开。开始的地方似乎是找到那个 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
这些应该是一样的。所以我很困惑。
【问题讨论】:
-
你的意思是
for i, each in enumerate(text_children):? -
它们应该是一样的。请记住,您只能遍历 text_children 一次,因为它将消耗生成器。
-
@gnibbler,感谢您指出我跳过了“枚举”——我在控制台中做得很好。我要澄清我的问题,但是......你是什么意思我只能迭代 text_children 一次?
标签: python beautifulsoup