I have a code that uses a while loop to print whatever is included in the
text = """ohsfhskfheifhsefis <a href = "fdnsfjsnfsnfns snkfsndfskj"</a>
<a href = "snfksnfsdf"</a>"""
index = 0
a = 0
b = 0
while index < len(text):
a = text.find('href', index)
b = text.find('/a', index)
index = index + 2
if index >= len(text):
Your search starts with
index set to
0, then finds the
href text at position 22. You then increment the index to
2, search again, and again find the text at position 22.
If you want to search to continue past the last match, you need to set
index to a position after the last match instead:
index = a + 1
Now the next
text.find() call starts searching at index 23 instead.
You'll also need to test if the text is not found:
if a < 0 or b < 0: break
Rather than manually search through text like this, consider using a HTML parser. Your search would be trivial with BeautifulSoup for example.