Home>
 <tr>
      <td class "a">
          
      </td>
      <td class "a">
          aaaa
      </td>
      <td class "a">
          bbbb
      </td>
      <td class "a">
          
            
          
      </td>
   </tr>
   <tr>
      <td class "a">
          
      </td>
      <td class "a">
          cccc
      </td>
      <td class "a">
          dddd
      </td>
      <td class "a">
          
            
          
      </td>
   </tr>


I'm studying Beautiful soup.
html = driver.page_source.encode ('utf-8')
soup = BeautifulSoup (html, "html.parser")

Parse as
Include bbbb.gif for these sourcesI want to take out the part surrounded by/is.

elements = soup.find_all ('img', src = re.compile ('bbbb.gif'))

Then, can be retrieved, but
How can I retrieve the part surrounded by the element (parent element?)/above it?

  • Answer # 1

    .parentIs the parent node.

    imgParentsaAnd that parenttd, And more parentstrSo

    img = soup.find ('img', src ='bbbb.gif')
    tr = img.parent.parent.parent


    Orfind_parentUsing,

    img = soup.find ('img', src ='bbbb.gif')
    tr = img.find_parent ('tr')


    find_allBecause there is no reason to usefind findI am doing it.

    In addition, the HTML you wrote is the first one.The end tag of/Notis. The same applies to others. furtherThere is no end tag for.