Beautiful Soup locating text after an <img> bracket

Viewed 25

I am quite new at using beautifulsoup and I am having a little trouble extracting text after an bracket.

<span content>"text"
    <img content src="/assets/images/photo.png">"Text I would like to grab"
    <span content class="classname">Text</span>
</span>

I am trying to extract the text after the photo but I cannot seem to grab it. Could anyone point me in the right direction

Many thanks :)

1 Answers

You can use find_next method by finding element img tag and extract text.

from bs4 import BeautifulSoup
soup=BeautifulSoup(html,"html.parser")
text1=soup.find("img").find_next(text=True).strip()

Output:

'"Text I would like to grab"'
Related