Ice Bear
Ice Bear

Reputation: 4086

Put list of links in correspond to img tags in a string in Python

I have here a simple string containing <img> tags & a list of example links:

s  = '''

<img src =''>
Is python a slow programming language because it's an interpreted one? :D

<img src =''>

I love python! but some say it's slow... Time to switch to Go or Julia?
<img src =''>

<img src =''>

'''

links = [
"https://www.google.com",
"https://www.facebook.com",
"https://www.twitter.com",
"https://www.youtube.com",
]

Now the main goal here is to put those links in correspond to those img tags. Something like this would be the output:

<img src ='https://www.google.com'>
Is python a slow programming language because it's an interpreted one? :D

<img src ='https://www.facebook.com'>

I love python! but some say it's slow... Time to switch to Go or Julia?
<img src ='https://www.twitter.com'>

<img src ='https://www.youtube.com'>

Solutions I searched for & tried

1.) Converting s into a bs4 object but to be honest I must not convert it to a bs4 object but I tried. I did what I searched about replace_with with bs4 & I get that output. The links were there right? but somehow it's not a proper img tag & the output now becomes a PURE html code, I just want it to be the same content as variable s but the links inserted or see provided sample output.

So using bs4 would not be really relevant but I'd be open to it. But for me to be honest, I would not want to have it converted to another object.

s  = '''

<img src =''>
Is python a slow programming language because it's an interpreted one? :D

<img src =''>

I love python! but some say it's slow... Time to switch to Go or Julia?
<img src =''>

<img src =''>

'''

links = [
"https://www.google.com",
"https://www.facebook.com",
"https://www.twitter.com",
"https://www.youtube.com",
]

soup = BeautifulSoup(s, 'html5lib')
imgs = soup.find_all("img")

#I could use zip() but let's take it this way
for idx,img in enumerate(imgs):
    img.replace_with(f'<img src="{links[idx]}"">')


print(soup)

Output:

<html><head></head><body>&lt;img src="https://www.google.com""&gt;
Is python a slow programming language because it's an interpreted one? :D

&lt;img src="https://www.facebook.com""&gt;

I love python! but some say it's slow... Time to switch to Go or Julia?
&lt;img src="https://www.twitter.com""&gt;

&lt;img src="https://www.youtube.com""&gt;

</body></html>

2.) Using the .split() method of str object won't work cause there would be times that the img tag would not have a space gap with the sentence or word, so it would be useless.

3.) I've come to think about the best way to do this is with regular expressions with the sample code solution here, however the only issue I am having is that this would only replace ONE img tag though it's a great solution! I learned & realised a lot from it. Also if regular expressions would be the best solution, I am having trouble implementing it.

Upvotes: 0

Views: 79

Answers (1)

R&#233;mi Desgrange
R&#233;mi Desgrange

Reputation: 908

One way could be to have placeholder and let python do the replcement for you if possible:

links = [
"https://www.google.com",
"https://www.facebook.com",
"https://www.twitter.com",
"https://www.youtube.com",
]

s  = '''

<img src ='{}'>
Is python a slow programming language because it's an interpreted one? :D

<img src ='{}'>

I love python! but some say it's slow... Time to switch to Go or Julia?
<img src ='{}'>

<img src ='{}'>

'''.format(*links)

Another way could be to use f-string (python3.6+):

google = "https://www.google.com",
facebook = "https://www.facebook.com",
twitter = "https://www.twitter.com",
youtube = "https://www.youtube.com",
]

s  = f'''

<img src ='{google}'>
Is python a slow programming language because it's an interpreted one? :D

<img src ='{facebook}'>

I love python! but some say it's slow... Time to switch to Go or Julia?
<img src ='{twitter}'>

<img src ='{youtube}'>

'''.format(*links)

Or use Jinja https://jinja.palletsprojects.com/en/2.11.x/ directly.

Upvotes: 1

Related Questions