我有一个小脚本,可以从.html文件中提取一些文本。
f = open(local_file,"r")
for line in f:
searchphrase = '<span class="position'
if searchphrase in line:
print("found it\n")
这对我来说没问题(错误处理稍后会导入),我的问题是我想要提取的文本跟搜索短语相隔两行。怎样才能在 .html 文件中向下移动两行?
我有一个小脚本,可以从.html文件中提取一些文本。
f = open(local_file,"r")
for line in f:
searchphrase = '<span class="position'
if searchphrase in line:
print("found it\n")
这对我来说没问题(错误处理稍后会导入),我的问题是我想要提取的文本跟搜索短语相隔两行。怎样才能在 .html 文件中向下移动两行?
您可以通过连续两次调用 next()
方法,将可迭代对象 f
推进两行:
with open(local_file,"r") as f
for line in f:
searchphrase = '<span class="position'
if searchphrase in line:
print("found it\n")
next(f) # skip 1 line
return next(f) # and return the line after that.
然而,如果你想解析HTML,请考虑使用HTML解析器代替。例如使用BeautifulSoup。
这对我很有效:
f = open(local_file,"r")
found = -1
for line in f:
if found == 2:
print("Line: "+line);
break
elif found > 0:
found += 1
else:
searchphrase = '<span class="position'
if searchphrase in line:
print("found it")
found = 1
bla
<span class="position">Hello</span>
blub
that's it
whatever
程序的输出结果:
found it
Line: that's it
你可以将break
替换为将found
重置为-1,以搜索模式的更多出现...