There is such a html code https://pastebin.com/wvuRtqzi

I want to pull out the first small tag from the img tag, but my attempts fail

For starters, I tried to get the img tag myself. I got it

find("img") 

Then I started looking for small in the img tag.

 find("img").find("small") 

But then they started popping up errors like this (errors because I tried other methods like ['small'] or trimming [: 1]

 AttributeError: 'NoneType' object has no attribute 'find' 

    2 answers 2

    <small> is inside the content attribute in the <img> element in a separate HTML document:

     soup = bs4.BeautifulSoup(HTML) img = soup.find('img', content=True) assert img is not None content_soup = bs4.BeautifulSoup(img['content']) print(content_soup.small.get_text()) 

      First, there can be no other tags inside the img tag. Secondly, in order to avoid errors, you need to read the documentation and understand what the find method returns. Thirdly, in my personal opinion, BeautifulSoup is a waste of time, because sometimes it generates a code that cannot be parsed by itself.

      • On account of the fact that in the img tag, others can not be told me everything. But on the website (not mine), this is exactly what was done and I need to parse it somehow - user210886
      • @ user210886 and in which div is the tag? - Pavel Durmanov