How do I get beautiful soup to get all the text between 2 IMG tags in any html web page

Joined
Jul 12, 2024
Messages
1
Reaction score
0
Hi

How do I get beautiful soup python library to get the text between any 2 IMG tags in any html5 web page?
All I know is I can use previouselement, nextelement and previoussibling and nextsibling.


Thanks,
Arvind.
 
Joined
Jul 4, 2023
Messages
448
Reaction score
54
There are several ways to get the text that is between any two <img> tags, it depends on the html code used on the page that contains this text.
Python:
from bs4 import BeautifulSoup

# Example 1
html1 = '''
<img src="image1.jpg" alt="First image">
<p>Lorem ipsum 1 text between any two img tags</p>
<img src="image2.jpg" alt="Second image">
'''

soup1 = BeautifulSoup(html1, 'html.parser')
text1 = soup1.find('p').text
print(text1)


# Example 2
html2 = '''
<img src="photo1.png" alt="Photo 1">
<div>Lorem ipsum 2 text between any two img tags</div>
<img src="photo2.png" alt="Photo 2">
'''

soup2 = BeautifulSoup(html2, 'html.parser')
text2 = soup2.find('div').text
print(text2)


# Example 3
html3 = '''
<div class="wrap">
  <img src="picture1.gif" alt="GIF Image 1">
  Lorem ipsum 3 text between any two img tags
  <img src="picture2.gif" alt="GIF Image 2">
</div>
'''

soup3 = BeautifulSoup(html3, 'html.parser')
div_wrap = soup3.find('div', class_='wrap')

# Remove the img tags
for img in div_wrap.find_all('img'):
    img.decompose()

# Get the text of the div and strip leading/trailing whitespace
text3 = div_wrap.get_text(strip=True)
print(text3)

1720862123162.png

What have you tried?
Do you have a url for the page?
I repeat this question too ... ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,870
Messages
2,569,918
Members
46,171
Latest member
A.N.Omalum

Latest Threads

Top