To regex for an HTML attribute value in Python, you can use the re
module.
Here's an example code snippet:
main.py364 chars16 lines
Output: https://www.example.com/
In the pattern
variable, we are regexing for the string href="
followed by any number of characters that are not "
and capturing them in a group using ()
. The final "
marks the end of the attribute value. We use re.search
to find this pattern in the HTML string and then retrieve the matched value from group(1).
gistlibby LogSnag