p_tags = '''
Full name Foobar
Born July 7, 1923, foo, bar
Current age 27 years 226 days
Major teams Japan, Jakarta, bazz, foo, foobazz
Also bar
foo style hand
bar style ball
foo position bak
这是我的Python代码,用美丽的汤:
def get_info(p_tags):
"""Returns brief information."""
head_list = []
detail_list = []
# This works fine
for head in p_tags.findAll('font', 'test-proof'):
head_list.append(head.contents[0])
# Some problem with this?
for index in xrange(2, 30, 4):
detail_list.append(p_tags.contents[index])
return dict([(l, detail_list[head_list.index(l)]) for l in head_list])
我从HTML获取正确的head_list但detail_list不工作。
head_list = [u'Full name',
u'Born',
u'Current age',
u'Major teams',
u'Also',
u'foo style',
u'bar style',
u'foo position']
我想是这样的
{
'Full name': 'Foobar',
'Born': 'July 7, 1923, foo, bar',
'Current age': '78 years 226 days',
'Major teams': 'Japan, Jakarta, bazz, foo, foobazz',
'Also': 'bar',
'foo style': 'hand',
'bar style': 'ball',
'foo position': 'bak'
}
任何帮助将是明显的。提前致谢。
2009-02-18
aatifh