I want to extract the text of an HTML file after the second occurrence of a specific tag.
I already tried regex and bs4, but I can't figure out what is going wrong. Regex always gives me only the hit itself without the rest of the html file and bs4 just does not work, because I don't know how to specify the end of the file for.
Simplified:
<html>
<veryspecific tag>
abc
</veryspecific tag>
<stuff that comes before>
</stuff that comes before>
<...
<veryspecific tag>
abc
</veryspecific tag>
<other tags that come after>
something
</other tags that come after>
</...>
<other tags that come after2>
something
</other tags that come after2>
</html>
#I tried splitting it, so I can take the last part which should contain the end of the file, starting from the latest occurrence, but it did not work:
htmltxt.split(r'abc.*$')
# I also tried to get the last tag and try to "while" over the 2 to get the text:
last_tag = html_parsed.findall('a')[-1]
while specific_tag != last_tag:
text = ...
specific_tag = specific_tag.next
I find the desired tag and can extract it, but I also need the rest of the file. Is there an easy and pythonic way to do it?