如何在html文件中搜索字符串?

时间:2018-07-16 03:11:29

标签: python html

我是Python的初学者。
我正在尝试使用Error语句在Report文件中搜索两个关键字htmlif。例如,下面是html文件,其中包含Error

<HEAD><STYLE TYPE="text/css">
.MSG_OK      { color:white; }
.MSG_SUCCESS { color:green; }
.MSG_WARNING { color:yellow; }
.MSG_ERROR   { color:red; }
.MSG_DEBUG   { color:blue; }
body         { background-color:black; }
</STYLE></HEAD>
<body><pre>
<span class=MSG_OK>Reserving ports for the test
</span><span class=MSG_OK>ABC test...
</span><span class=MSG_ERROR>Error: xxx resource is already in use.
 Error with xxx....
</span><span class=font>(A)bort, (R)etry, (I)gnore?</span>

我在文件对象中使用了read(),但是它不起作用。我的代码:

    html_path = "D:\\abcd.html"

    with open(html_path) as html_file:
        print(html_file.read())

        #for line in html_file.read():
        if "Error" in html_file.read():
            print("[error occur")
            html_file.close()

        elif "Report" in html_file.read():
            print("get result")
            html_file.close()
        else:
            print("[[[[nothing]]]]")

我总是得到结果:

<HEAD><STYLE TYPE="text/css">
.MSG_OK      { color:white; }
.MSG_SUCCESS { color:green; }
.MSG_WARNING { color:yellow; }
.MSG_ERROR   { color:red; }
.MSG_DEBUG   { color:blue; }
body         { background-color:black; }
</STYLE></HEAD>
<body><pre>
<span class=MSG_OK>Reserving ports for the test
</span><span class=MSG_OK>ABC test...
</span><span class=MSG_ERROR>Error: xxx resource is already in use.
 Error with xxx....
</span><span class=font>(A)bort, (R)etry, (I)gnore?</span>
[[[[nothing]]]]

在我的Error语句中找不到类似两个关键词Reportif的情况。所以我总是得到结果[[[[nothing]]]]。有人可以纠正我的代码并告诉我原因吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

除非重新开始,否则您只能读取一次文件。但这不是您想要的方式。

逐行迭代文件,检查条件。

error = False
report = False

with open(html_path) as html_file:
  for line in html_file:
    print(line)
    if 'Error' in line:
      error = True
    if 'Report' in line:
      report = True
    print(line)
  else:
    if error:
      print('error')
    elif report:
      print('result')
    else:
      print('nothing')