Home > Software engineering >  Find all <a href> HTML tags and append target blank values using Python regular expression
Find all <a href> HTML tags and append target blank values using Python regular expression

Time:11-21

I want to find all

<a href='https://example.com/'>

references in a large file and append the

target='_blank' rel='noopener noreferrer'

option to the end of the tag, if it is missing.

Roughly, I did the following:

re.sub(r'<a href=([^>] )', r'<a href=([^>] )'   " target='_blank' rel='noopener noreferrer'", content)

Note: content contains the body of text to alter.

But, the second argument, which should be the value to replace is messing up the result.

The output I am getting is:

<a href=([^>] ) target='_blank' rel='noopener noreferrer'>

The expected result should be:

<a href='https://example.com/' target='_blank' rel='noopener noreferrer'>

What am I doing incorrectly, and how do I fix this issue?

CodePudding user response:

Try this: (*** If coding professionally, use the tool ti7 suggested.)

import re
content = "<a href='https://example.com/'>"
x = re.sub(r'(<a href=([^>] ))', r'\1'   " target='_blank' rel='noopener noreferrer'", content)
print(x)

output:
   <a href='https://example.com/' target='_blank' rel='noopener noreferrer'>

CodePudding user response:

If you can use a 3rd-party library, BeautifulSoup may work very well for you!
https://www.crummy.com/software/BeautifulSoup/bs4/doc/

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_contents, "html.parser")
soup.find_all("a")
  • Related