<p>这不是最有效的方法,但您可以尝试:</p>
<pre><code>file = open(File_Path,'r') #open my file ( be careful with encoding)
text = file.readlines() #extract the content of the file
file.close() #close my file
Goal = [] # will include all the lines beetwen Executives and Analysts
for indice,line in enumerate(text):
if "<p><strong>Executives</strong></p>" in line:
"""
when the line with "<p><strong>Executives</strong></p>" is found, it will add to Goal all the next line until <p><strong>Analysts</strong></p> appear in a line
"""
i = 1
while not("<p><strong>Analysts</strong></p>" in text[indice+i]):
Goal.append(text[indice+i])
i +=1
break
print(Goal)
</code></pre>
<p>最重要的部分在主循环中,因此您可以根据自己的程序调整它</p>
<p>如果您知道高管和分析师之间的行数,您可以通过以下方式替换while循环:</p>
<pre><code>Goal = text[indice+1:indice+<number_of_line + 1>]
</code></pre>
<p>并删除:i=1</p>
<p>通过这种方式,您可以在所有行中保留标记(如:<;p>;..<;/p>;)和“\n”</p>
<p>您可以使用内置函数删除一行中的所有“\n”:</p>
<pre><code>line = line.replace("\n","")
</code></pre>
<p>在标记之间获取数据的方法很少,例如在HTMLPasser中使用handle_数据,或者您可以在re中使用findall函数:</p>
<pre><code>data_in_line = re.findall(r'>(.*?)<',line)
</code></pre>
<p>“行中的数据”将是关于模式r'>;的所有数据的列表;(.*)<;'因此,所有的数据都在“>;”之间和“<;”</p>
<p>举个例子:'<;p>;atest</p>;'</p>
<p>它将返回['atest']</p>
<p>这对你有帮助吗</p>