self.last_link = None

def handle_starttag(self, tag, attrs):
    attrs = dict(attrs)
if 'href' in attrs:
    self.last_link = attrs['href']
content = """<html><a href="http://www.not-this-one.com"></a>
<a href="http://www.somewebsite.com"></a><p>other stuff</p><p>sales</p>
</html>
"""
idx = content.find('sales')
with contextlib.closing(MyParser()) as parser:
    parser.feed(content[:idx])
print(parser.last_link)
Using Regex to Search for HTML links near keywords