简体   繁体   中英

BeautifulSoup can't find a tag by its class

Here is the part of the web page:

 <div class="MPinfo">
     <ul class="frontList">
         <li>some text</li>
         <li>some text</li>
         <li>some text</li>
         <li>some text</li>
         <li>some text</li>
         <li>some text
             <a href="/some_local_link/8976">some text</a>;
             <a href="/some_local_link/8943">some text</a>;
         </li>
         <li>E-mail: 
             <a href="mailto:Ss.Sssssss@mail.com">Ss.Sssssss@mail.com</a>
         </li>
     </ul>
 </div>

I am trying to get the div by its class and then extract the email link just to email itself like: Ss.Sssssss@mail.com

page = urllib.urlopen(link)
soup = BeautifulSoup(page.read())
print soup.find('div', attrs={'class': 'MPinfo'})

I have tried several ways to get the div but it returns empty list or None

You can select all li under the div, it will be a list, so you can select last li element like [-1]

>>> soup.find("div",attrs={"class":"MPinfo"}).find_all("li")[-1].a.text
'Ss.Sssssss@mail.com'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM