我解析一些HTML表单与美丽的汤。基本上我大约有60个输入字段大多是单选按钮和复选框。到目前为止,这适用于以下代码:联想列表python
from BeautifulSoup import BeautifulSoup
x = open('myfile.html','r').read()
out = open('outfile.csv','w')
soup = BeautifulSoup(x)
values = soup.findAll('input',checked="checked")
# echoes some output like ('name',1) and ('value',4)
for cell in values:
# the following line is my problem!
statement = cell.attrs[0][1] + ';' + cell.attrs[1][1] + ';\r'
out.write(statement)
out.close()
x.close()
正如代码,表明我的问题IST在属性选择,因为HTML模板是丑陋的,混淆的是属于输入字段参数的顺序。我对name =“somenumber”value =“someothernumber”感兴趣。不幸的是,我的attrs [1]方法不起作用,因为name和value不会在我的html中以相同的顺序出现。
有什么办法可以关联地访问生成的BeautifulSoup列表吗?
Thx提前任何建议!
对我来说,一个循环就够了..请看我的评论给彼得的答案。 – 2010-06-29 18:49:15