Search code examples
pythonhref

how to retrieve text from anchor href attribute in python


Let's say I have a link like this:

link = '<a href="some text">...</a>'

Is there any way I can retrieve the text from anchor href attribute so the result will be something like this:

hrefText = 'some text'

And thank you in advance


Solution

  • This is a way:

    import re
    print re.search('(?<=<a href=")[^"]+',link).group(0)
    

    Or,

    print re.search(r'<a\s+href="([^"]+)',link).group(1)