我有两个输入文件:一个html和一个css。我想根据css文件的内容对html文件进行一些操作。嵌套for循环迭代停止
我的HTML是这样的:(!分别为每个跨度ID)
<html>
<head>
<title></title>
</head>
<body>
<p class = "cl1" id = "id1"> <span id = "span1"> blabla</span> </p>
<p class = "cl2" id = "id2"> <span id = "span2"> blablabla</span> <span id = "span3"> qwqwqw </span> </p>
</body>
</html>
风格跨度ID在CSS文件中定义
立足于做真正的东西(跨度删除之前他们样式)我只是想从HTML打印出ID和从每个ID对应的CSS风格descritption。
代码:
from lxml import etree
tree = etree.parse("file.html")
filein = "file.css"
def f1():
with open(filein, 'rU') as f:
for span in tree.iterfind('//span'):
for line in f:
if span and span.attrib.has_key('id'):
x = span.get('id')
if "af" not in x and x in line:
print x, line
def main():
f1()
所以,有两个for循环,它遍历完美,如果分开了,但如果这个功能放在一起,第一循环之后的迭代停止:
>> span1 span`#span1 { font-weight: bold; font-size: 11.0pt; font-style: normal; letter-spacing: 0em }
我怎样才能解决这个问题?
谢谢!它完美的作品:) – user3241376 2014-11-05 15:42:39